1

I've been playing with this code all day and I can't seem to understand what's going on with my X-axis. Most of the code is from here

This is my first legitimate time trying to actually use fft in real life so sorry if I've totally screwed everything up!

The code is working at least 90%. I am able to differentiate between a stable signal and one with an oscillation.

Known Good: Known Good, no obvious peaks

Known Bad: Known Bad, obvious peak

The issue for me is that I feel like my X scale is arbitrary. As in, I know how to set it but for the life of me I can't figure out why the "correct" result should be what it is.

The code:

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
import xlrd

start = 5 #start number in xl
end = 593 #end number from xl

fig = plt.figure(figsize=[14,4])
N = (end-start)           # Number of samplepoints
Fs = 10*N        #I believe this is where my troubles are coming from, i don't know what it's supposed to be!
T = 1.0 / 3.175*np.pi      # N_samps*T (#samples x sample period) is the sample spacing.
N_fft = 250 # Number of bins (chooses granularity)
x = np.linspace(0, N*T, N)     # the interval
#y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)   # the test signal
workbook = xlrd.open_workbook('MY_FILE_LOCATION') #put actual file loc 
worksheet = workbook.sheet_by_name('Sheet1')
y = worksheet.col_values(12, start, end)# data loc in xl

#add a known signal to the system
#This is how I got the "correct" answer
# for i in range(len(y)):
#     if i % 10 == 0:
#         y[i] += 1


# removing the mean of the signal
mean_removed = np.ones_like(y)*np.mean(y) 
y = y - mean_removed

# Compute the fft.
yf = scipy.fftpack.rfft(y,n=N_fft)
xf = np.arange(0,Fs,Fs/N_fft) #not clear what this is supposed to be doing

##### Plot the fft #####
ax = plt.subplot(111)
pt, = ax.plot(xf,np.abs(yf), lw=1.0, c='b')
ax.set_xlim((ax.get_xlim()[0],Fs))
ax.set_title('FFT', fontsize= 16, fontweight="bold")
ax.set_ylabel('Strength')
ax.set_xlabel('Length')
ax.grid()
plt.show()

So what I tried to do was add an artificial signal to see what would happen. This is what I got: Strong signal added at regular interval

OK, great. My signal is there but it's supposed to be showing up (roughly) every 100 units. And in order to do that I have to set that Fs variable to ~500. Which I don't really understand.

500 just doesn't show up in anything that I'm doing. So I just can't picture where it should be coming from.

Maybe someone can help me understand what I'm missing or just point me in the right direction for a better understand of practical applications of the fft. I feel like I've seen enough examples of folks sampling a known function, executing the fft and then exclaiming: "see, it works!"

What are some good ways to deal with dirty data? (I've read something about Welch which I assume is the first place to look)

Would it be worth it to try to get more data? theoretically smoother data (I would assume that Fourier would not fit well to spikes)?

d6stringer
  • 71
  • 1
  • 10

0 Answers0