The personal website of Scott W Harden
July 12th, 2010

Coder's Block

I like the idea of writing a set of tools for scientific frequency analysis (more than just turning audio into images), and I keep starting over re-coding things from scratch. I develop too much, too quickly, and half way in I get overwhelmed and mentally blocked. I do it to myself. I've taken about a week off and will continue to take a few more days off to reset my mind. I'm trying to improve my coding by reading books (e-books) about advanced Python programming. Perhaps when it's time to return, I'll write gorgeous and functional code. I always seem to have one or the other, but never both [sigh]

The photo above is the signal of my (AJ4VD) little homemade transmitter in Gainesville, Florida, USA (using a 20-ft piece of wire inside my apartment as an antenna) detected by ON5EX in Belgium. It makes me happy. It reminds me that some of the projects I work on succeed, which gives me motivation to continue pursuing the ones which currently challenge me.

Markdown source code last modified on January 18th, 2021
---
title: Coder's Block
date: 2010-07-12 22:36:03
tags: qrss
---

# Coder's Block

__I like the idea__ of writing a set of tools for scientific frequency analysis (more than just turning audio into images), and I keep starting over re-coding things from scratch. I develop too much, too quickly, and half way in I get overwhelmed and mentally blocked. I do it to myself. I've taken about a week off and will continue to take a few more days off to reset my mind. I'm trying to improve my coding by reading books (e-books) about advanced Python programming. Perhaps when it's time to return, I'll write gorgeous and functional code. I always seem to have one or the other, but never both \[sigh\]

<div class="text-center img-border">

![](qrss_aj4vd_belgium.jpg)

</div>

__The photo above is__ the signal of my (AJ4VD) little homemade transmitter in Gainesville, Florida, USA (using a 20-ft piece of wire inside my apartment as an antenna) detected by ON5EX in Belgium. It makes me happy. It reminds me that some of the projects I work on succeed, which gives me motivation to continue pursuing the ones which currently challenge me.

June 23rd, 2010

Insights Into FFTs, Imaginary Numbers, and Accurate Spectrographs

I'm attempting to thoroughly re-write the data assessment portions of my QRSS VD software, and rather than rushing to code it (like I did last time) I'm working hard on every step trying to optimize the code. I came across some notes I made about Fast Fourier Transformations from the first time I coded the software, and though I'd post some code I found helpful. Of particular satisfaction is an email I received from Alberto, I2PHD, the creator of Argo (the "gold standard" QRSS spectrograph software for Windows). In it he notes:

I think that [it is a mistake to] throw away the imaginary part of the FFT. What I do in Argo, in Spectran, in Winrad, in SDRadio and in all of my other programs is compute the magnitude of the [FFT] signal, then compute the logarithm of it, and only then I do a mapping of the colors on the screen with the result of this last computation.

Alberto, I2PHD (the creator of Argo)

UPDATE IN SEPTEMBER, 2020 (10 years later): I now understand that magnitude = sqrt(real^2 + imag^2) and this post is a bit embarrassing to read! Check out my .NET FFT library FftSharp for a more advanced discussion on this topic.

These concepts are simple to visualize when graphed. Here I've written a short Python script to listen to the microphone (which is being fed a 2kHz sine wave), perform the FFT, and graph the real FFT component, imaginary FFT component, and their sum. The output is:

Of particular interest to me is the beautiful complementary of the two curves. It makes me wonder what types of data can be extracted by the individual curves (or perhaps their difference?) down the road. I wonder if phase measurements would be useful in extracting weak carries from beneath the noise floor?

Here's the code I used to generate the image above. Note that my microphone device was set to listen to my stereo output, and I generated a 2kHz sine wave using the command speaker-test -t sine -f 2000 on a PC running Linux. I hope you find it useful!

import numpy
import pyaudio
import pylab
import numpy

### RECORD AUDIO FROM MICROPHONE ###
rate = 44100
soundcard = 1  # CUSTOMIZE THIS!!!
p = pyaudio.PyAudio()
strm = p.open(format=pyaudio.paInt16, channels=1, rate=rate,
              input_device_index=soundcard, input=True)
strm.read(1024)  # prime the sound card this way
pcm = numpy.fromstring(strm.read(1024), dtype=numpy.int16)

### DO THE FFT ANALYSIS ###
fft = numpy.fft.fft(pcm)
fftr = 10*numpy.log10(abs(fft.real))[:len(pcm)/2]
ffti = 10*numpy.log10(abs(fft.imag))[:len(pcm)/2]
fftb = 10*numpy.log10(numpy.sqrt(fft.imag**2+fft.real**2))[:len(pcm)/2]
freq = numpy.fft.fftfreq(numpy.arange(len(pcm)).shape[-1])[:len(pcm)/2]
freq = freq*rate/1000  # make the frequency scale

### GRAPH THIS STUFF ###
pylab.subplot(411)
pylab.title("Original Data")
pylab.grid()
pylab.plot(numpy.arange(len(pcm))/float(rate)*1000, pcm, 'r-', alpha=1)
pylab.xlabel("Time (milliseconds)")
pylab.ylabel("Amplitude")
pylab.subplot(412)
pylab.title("Real FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, fftr, 'b-', alpha=1)
pylab.subplot(413)
pylab.title("Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, ffti, 'g-', alpha=1)
pylab.subplot(414)
pylab.title("Real+Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, fftb, 'k-', alpha=1)
pylab.show()

After fighting for a while long with a "shifty baseline" of the FFT, I came to another understanding. Let me first address the problem. Taking the FFT of different regions of the 2kHz wave I got traces with the peak in the identical location, but the "baselines" completely different.

Like many things, I re-invented the wheel. Since I knew the PCM values weren't changing, the only variable was the starting/stopping point of the linear sample. "Hard edges", I imagined, must be the problem. I then wrote the following function to shape the PCM audio like a triangle, silencing the edges and sweeping the volume up toward the middle of the sample:

def shapeTriangle(data):
    triangle=numpy.array(range(len(data)/2)+range(len(data)/2)[::-1])+1
    return data*triangle

After shaping the data BEFORE I applied the FFT, I made the subsequent traces MUCH more acceptable. Observe:

Now that I've done all this experimentation/thinking, I remembered that this is nothing new! Everyone talks about shaping the wave to minimize hard edges before taking the FFT. They call it windowing. Another case of me re-inventing the wheel because I'm too lazy to read others' work. However, in my defense, I learned a lot by trying all this stuff -- far more than I would have learned simply by copying someone else's code into my script. Experimentation is the key to discovery!

Markdown source code last modified on January 18th, 2021
---
title: Insights Into FFTs, Imaginary Numbers, and Accurate Spectrographs
date: 2010-06-23 22:21:00
tags: qrss, python, old
---

# Insights Into FFTs, Imaginary Numbers, and Accurate Spectrographs

__I'm attempting to thoroughly re-write the data assessment__ portions of my QRSS VD software, and rather than rushing to code it (like I did last time) I'm working hard on every step trying to optimize the code. I came across some notes I made about Fast Fourier Transformations from the first time I coded the software, and though I'd post some code I found helpful. Of particular satisfaction is an email I received from Alberto, I2PHD, the creator of Argo (the "gold standard" QRSS spectrograph software for Windows). In it he notes:

<blockquote class="wp-block-quote"><p>I think that [it is a mistake to] throw away the imaginary part of the FFT. What I do in Argo, in Spectran, in Winrad, in SDRadio and in all of my other programs is compute the magnitude of the [FFT] signal, then compute the logarithm of it, and only then I do a mapping of the colors on the screen with the result of this last computation.</p><cite> Alberto, I2PHD (the creator of Argo)</cite></blockquote>

> __UPDATE IN SEPTEMBER, 2020 (10 years later):__ I now understand that `magnitude = sqrt(real^2 + imag^2)` and this post is a bit embarrassing to read! Check out my .NET FFT library [FftSharp](https://github.com/swharden/FftSharp) for a more advanced discussion on this topic.

__These concepts are simple__ to visualize when graphed. Here I've written a short Python script to listen to the microphone (which is being fed a 2kHz sine wave), perform the FFT, and graph the real FFT component, imaginary FFT component, and their sum. The output is:

<div class="text-center">

[![](real_imaginary_fft_pcm_thumb.jpg)](real_imaginary_fft_pcm.png)

</div>

__Of particular interest__ to me is the beautiful complementary of the two curves. It makes me wonder what types of data can be extracted by the individual curves (or perhaps their difference?) down the road. I wonder if phase measurements would be useful in extracting weak carries from beneath the noise floor?

<div class="text-center">

[![](fft_base2_thumb.jpg)](fft_base2.png)

</div>

__Here's the code I used to generate the image above.__ Note that my microphone device was set to listen to my stereo output, and I generated a 2kHz sine wave using the command `` speaker-test -t sine -f 2000 `` on a PC running Linux. I hope you find it useful!

```python
import numpy
import pyaudio
import pylab
import numpy

### RECORD AUDIO FROM MICROPHONE ###
rate = 44100
soundcard = 1  # CUSTOMIZE THIS!!!
p = pyaudio.PyAudio()
strm = p.open(format=pyaudio.paInt16, channels=1, rate=rate,
              input_device_index=soundcard, input=True)
strm.read(1024)  # prime the sound card this way
pcm = numpy.fromstring(strm.read(1024), dtype=numpy.int16)

### DO THE FFT ANALYSIS ###
fft = numpy.fft.fft(pcm)
fftr = 10*numpy.log10(abs(fft.real))[:len(pcm)/2]
ffti = 10*numpy.log10(abs(fft.imag))[:len(pcm)/2]
fftb = 10*numpy.log10(numpy.sqrt(fft.imag**2+fft.real**2))[:len(pcm)/2]
freq = numpy.fft.fftfreq(numpy.arange(len(pcm)).shape[-1])[:len(pcm)/2]
freq = freq*rate/1000  # make the frequency scale

### GRAPH THIS STUFF ###
pylab.subplot(411)
pylab.title("Original Data")
pylab.grid()
pylab.plot(numpy.arange(len(pcm))/float(rate)*1000, pcm, 'r-', alpha=1)
pylab.xlabel("Time (milliseconds)")
pylab.ylabel("Amplitude")
pylab.subplot(412)
pylab.title("Real FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, fftr, 'b-', alpha=1)
pylab.subplot(413)
pylab.title("Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, ffti, 'g-', alpha=1)
pylab.subplot(414)
pylab.title("Real+Imaginary FFT")
pylab.xlabel("Frequency (kHz)")
pylab.ylabel("Power")
pylab.grid()
pylab.plot(freq, fftb, 'k-', alpha=1)
pylab.show()
```

__After fighting for a while long with__ a "shifty baseline" of the FFT, I came to another understanding. Let me first address the problem. Taking the FFT of different regions of the 2kHz wave I got traces with the peak in the identical location, but the "baselines" completely different.

<div class="text-center">

[![](fft_base3_thumb.jpg)](fft_base3.png)

</div>

__Like many things, I re-invented the wheel.__ Since I knew the PCM values weren't changing, the only variable was the starting/stopping point of the linear sample. "Hard edges", I imagined, must be the problem. I then wrote the following function to shape the PCM audio like a triangle, silencing the edges and sweeping the volume up toward the middle of the sample:

```python
def shapeTriangle(data):
    triangle=numpy.array(range(len(data)/2)+range(len(data)/2)[::-1])+1
    return data*triangle
```

__After shaping the data BEFORE I applied the FFT,__ I made the subsequent traces MUCH more acceptable. Observe:

__Now that I've done all this experimentation/thinking,__ I remembered that this is nothing new! Everyone talks about shaping the wave to minimize hard edges before taking the FFT. They call it _windowing._ Another case of me re-inventing the wheel because I'm too lazy to read others' work. However, in my defense, I learned a lot by trying all this stuff -- far more than I would have learned simply by copying someone else's code into my script. Experimentation is the key to discovery!
June 12th, 2010

MEPT Insulation Improves Stability

While it may not be perfect, it's a whole lot better. Below is a capture from this morning of my signal (the waves near the bottom). Compare that to how it was before and you should notice a dramatic improvement! The MEPT is inside a metal box inside a 1-inch-thick Styrofoam box. Very cool!

Markdown source code last modified on January 18th, 2021
---
title: MEPT Insulation Improves Stability
date: 2010-06-12 09:18:14
tags: qrss
---

# MEPT Insulation Improves Stability

__While it may not be perfect__, it's a whole lot better. Below is a capture from this morning of my signal (the waves near the bottom). Compare that to how it was before and you should notice a dramatic improvement! The MEPT is inside a metal box inside a 1-inch-thick Styrofoam box. Very cool!

<div class="text-center img-border">

![](stable.jpg)

</div>

<div class="text-center img-border">

[![](assembled-squished_thumb.jpg)](assembled-squished.jpg)

</div>

June 10th, 2010

QRSS Receiver Works... Barely

I completed work on my first RF receiver, and for what it is it seems to work decently. It should be self-explanatory from the photos. It's based around an SA602. As with everything, I don't plan on posting schematics until the project is complete because I don't want people re-creating junky circuits! It's stationed at the University of Florida's club station W4DFU and its spectrograph can be viewed in real time from the QRSS VD - Web Grabber - W4DFU page.

Markdown source code last modified on January 18th, 2021
---
title: QRSS Receiver Works... Barely
date: 2010-06-10 23:27:40
tags: amateur radio, qrss
---

# QRSS Receiver Works... Barely

__I completed work on my first RF receiver__, and for what it is it seems to work decently. It should be self-explanatory from the photos. It's based around an SA602. As with everything, I don't plan on posting schematics until the project is complete because I don't want people re-creating junky circuits! It's stationed at the University of Florida's club station W4DFU and its spectrograph can be viewed in real time from the [__QRSS VD - Web Grabber - W4DFU__](http://ham.w4dfu.ufl.edu:8080/qrss_vd/website/) page.

<div class="text-center img-border img-medium">

[![](IMG_3475_thumb.jpg)](IMG_3475.jpg)

[![](IMG_3482_thumb.jpg)](IMG_3482.jpg)

[![](IMG_34792_thumb.jpg)](IMG_34792.jpg)

[![](dc_qrss_thumb.jpg)](dc_qrss.jpg)

[![](capture_thumb.jpg)](capture.jpg)

</div>

June 9th, 2010

Minimalist Radio Receiver

Now that my minimalist QRSS transmitter is mostly functional, I'm shifting gears toward building a minimalist receiver. These are some early tests, but I'm amazed I managed to hack something together that actually works! Once it's finished I'll post schematics. For now, here are some photos. This receiver is based upon an SA602 and although there *IS* an op-amp on the board, I actually bypassed it completely! The SA602 seems to put out enough juice to make my PC microphone jack happy, and those cheap op-amps are noisy anyway, so awesome! Go minimalism!

Here's the output from 7.040 MHz. Conditions are pretty bad right now, and I'm at my apartment using my crazy indoor antenna

Markdown source code last modified on January 18th, 2021
---
title: Minimalist Radio Receiver
date: 2010-06-09 23:42:00
tags: qrss, amateur radio, old
---

# Minimalist Radio Receiver

__Now that my minimalist QRSS transmitter is mostly functional, I'm shifting gears toward building a minimalist receiver.__ These are some early tests, but I'm amazed I managed to hack something together that actually works! Once it's finished I'll post schematics. For now, here are some photos. This receiver is based upon an SA602 and although there \*IS\* an op-amp on the board, I actually bypassed it completely! The SA602 seems to put out enough juice to make my PC microphone jack happy, and those cheap op-amps are noisy anyway, so awesome! Go minimalism!

<div class="text-center img-border img-medium">

[![](DSCN0832_thumb.jpg)](DSCN0832.jpg)

[![](DSCN0833_thumb.jpg)](DSCN0833.jpg)

</div>

__Here's the output from 7.040 MHz.__ Conditions are pretty bad right now, and I'm at my apartment using my crazy indoor antenna

<div class="text-center img-border img-medium">

[![](recvbig_thumb.jpg)](recvbig.jpg)

</div>

Pages