1

I have some complex data (small bandwidth around a set frequency) that I'm curious to plot, but I am slightly lost as to how should I proceed about interpreting a complex signal sampled in a particular range.

So, for instance, here is the code (and my rather poor attempt at the problem) that I wrote so I have a clean example to experiment with artificial signal, that generates complex representation of 78 KHz wave. What I am trying to do is to get a plot that is centred at 120 KHz, and spans from 70 to 170 KHz, mimicking narrow sampling range of the real receiver.

import numpy as np
import matplotlib.pyplot as plt

#sampling rate, samples/second; 100 KHz
rate = 100*10**3
#sample spacing in time, seconds/sample
interval = np.true_divide(1, rate)
#length of the fourier transform
n = 256
#time vector
t = np.linspace(0.0, n*interval, n)

#frequency of artificial signal; 78 KHz
f = 78*10**3
#complex signal
s = np.exp(1j*2*np.pi*f*t)

#dft of the data
dft = np.fft.fft(s)
#frequency bins
x = np.fft.fftfreq(n, d=interval)
#center zero-frequency component in data; take absolute values
dft = np.abs(np.fft.fftshift(dft))
#center zero frequency component in bins; naively add the center frequency, 120 KHz
x = np.fft.fftshift(x) + 120*10**3

plt.plot(x, dft)
plt.show()

The output, is wrong, as expected with the crude attempt to mimic particular frequency range.

Plot made by the code snippet above

P.S. Different plot , with f = 88*10*83 - why has the magnitude changed here, suddenly?

Edit: My post has been marked for duplicated with a topic related purely to plotting, while what I'm actually after is processing and/or inversion of bandpass-filtered data.

2
  • No, that's not a duplicate of that for sure. We're talking about NumPy/SciPy processing of complex time-domain data recorded through a bandpass filter .
    – roland254
    Jun 5, 2016 at 7:46
  • If I remember correctly about FT, it's the integral that's conserved, not the peak magnitude. So that answers your side-question: the second peak is narrower, so it must be higher.
    – Mark
    Jun 5, 2016 at 8:01

1 Answer 1

8

Nyquist Frequency and Aliasing

Your signal should be a (complex) exponential oscillation at +78 kHz, sampled at 100 kHz. This doesn't work. What you see instead is an alias frequency at -22 kHz (78 kHz - 100 kHz). You have to make sure that no frequency of your signal is higher than half your sampling frequency. For a signal of 78 kHz take a sample frequency of 200 kHz, for example.

import numpy as np
from matplotlib import pyplot as plt

sample_frequency = 200e3  # 200 kHz
sample_interval = 1 / sample_frequency
samples = 256  # you don't necessarily have to use a power of 2
time = np.linspace(0, samples*sample_interval, samples)

signal_frequency = 78e3  # 78 kHz
signal = np.exp(2j*np.pi*signal_frequency*time)

FFT and fftfreq

np.fft.fftfreq already returns the right frequencies, adding a "center frequency" mekes no sense. Don't do it.

signal_spectrum = np.fft.fftshift(np.fft.fft(signal))
freqs = np.fft.fftshift(np.fft.fftfreq(samples, d=sample_interval))

Plotting

The plotting part of your question is only about setting the axes. Use plt.xlim.

plt.figure(figsize=(10,5))
plt.plot(freqs / 1e3, np.abs(signal_spectrum))  # in kHz
plt.xlim(70, 170)

Signal spectrum

The plotted line ends just before 100 kHz, because as mentioned above your signal cannot have a frequency part higher than your half sample frequency.

Magnitude of spectrum

Since your signal is time-discrete (several single samples, not a continuous function), your spectrum is continuous. The Discrete Fourier Transform, however, only returns discrete samples of the continuous spectrum. If you would fit a curve through the sampling points, its peak would have the same magnitude for different frequencies.

Alternatively, you could increase the number of FFT sampling points by zero-padding your signal (take a look at the numpy.fft.fft documentation):

signal_spectrum = np.fft.fftshift(np.fft.fft(signal, 10*samples))
freqs = np.fft.fftshift(np.fft.fftfreq(10*samples, d=sample_interval))

plt.figure(figsize=(10,5))
plt.plot(freqs / 1e3, np.abs(signal_spectrum))  # in kHz
plt.xlim(65, 95).
plt.grid()

Spectrum of zero-padded signal with leakage

If you're asking yourself why the spectrum looks so rippled, Take a look at spectral leakage.

1
  • Thanks for the answer, but I think I'll take it to DSPSE, since answer of the sort you provided is not necessarily helpful.
    – roland254
    Jun 5, 2016 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.