Dudi b
Dudi b

Reputation: 260

Numpy FFT over few seconds

I'm trying to understand how fft in python works (or in general).
When I have a signal that is recorded for a few seconds I can only display one second of that FFT.
Is there a way to take all of the data and averaged this out? I did this before in LabView. Can any one help?

Here is an example code that I'm using.

from numpy import linspace, sin, pi, log10, average, arange
from matplotlib.pyplot import plot, show, figure
from numpy.fft import fft, fftfreq

N = 1000 * 60 * 4
dt = 1 / 1000
x = linspace(0, N*dt, N)
freq = linspace(10, 200, N)
sinsweep = sin(x*freq)
mavg = [average(sinsweep[i*60:(i+1)*60]) for i in range(int(N/60))]
plot(freq, sinsweep, '.')
plot(linspace(10, 200, int(N/60)), mavg, '.')

f = figure()
t = arange(60)
sp = fft(mavg, n=60)
freq = fftfreq(t.shape[-1])
plot(sp.imag)
show()

plot data fft

Upvotes: 1

Views: 562

Answers (1)

B. M.
B. M.

Reputation: 18628

I give some modification to your code, to obtain a beautiful spectrum. First I increase the number of points to verify Shannon criterion. And some tricks to
improve speed.

from numpy import linspace, sin, pi, log10, average, arange
from matplotlib.pyplot import plot, show, figure
from numpy.fft import fft, fftfreq
close()
N = 10000 * 60 * 4
dt = 1 / 10000
t = arange(0, N*dt, dt)
freq = linspace(10, 200, N)
sinsweep = sin(t*freq)
mavg = sinsweep.reshape(-1,60).mean(1)
tm=t[::60]
figure('signal')
plot(tm, mavg)    
sp = fft(mavg)
freq = fftfreq(tm.size,dt*60)
valid=freq>0
figure('spectrum')
plot(freq[valid],abs(sp[valid]))
show()

forenter image description here

Is it what you expected ?

Upvotes: 1

Related Questions