Hello Matlab community,
I am trying to wrap my head around sampling frequencies and the FFT code. I have used the following example: https://www.mathworks.com/help/matlab/ref/fft.html
The code I copied from the link above provides results which seem to have the correct amplitudes (0.7 and 1) but danse around the exact frequency (49.3-50.7 and 119.3-120.7 ish). The exact code I used is:
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);f = Fs*(0:(L/2))/L;Y = fft(S);P2 = abs(Y/L);P1 = P2(1:L/2+1);P1(2:end-1) = 2*P1(2:end-1);plot(f,P1) title('Single-Sided Amplitude Spectrum of S(t)')xlabel('f (Hz)')ylabel('|P1(f)|')
Wherease the code I developed using what I think is logical (based on my limited programming knowledge) gives almost the exact frequencies (50 and 120, but tapper outward below 0.025 amplitude), but reduced amplitudes (0.6971 and 0.9765):
Fs = 1000; % Sampling frequency T = 1/Fs; % Sampling period L = 1500; % Length of signalfor t=0:T:L S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); x(round(t/T)+1,1)=S; time(round(t/T)+1,1)=t; f(round(t/T)+1,1) = Fs*t/L;endf1 = f(1:Fs*L/2+1);fftx = fft(x);P2 = abs(fftx/(Fs*L));P1 = 2*P2(1:Fs*L/2+1);plot(f1,P1)xlabel('Frequency [Hz]')ylabel('Amplitude [V]')title('FFT')
What gives?? In my mind these codes seem equivalent… Also, what does the following line in the example provided in the link above mean:
P1(2:end-1) = 2*P1(2:end-1);
It seems to give the same results whether that line of code is there or not…
Best Answer