FFT from a .CSV not giving the correct results

2 views (last 30 days)
%Below is the code. The file is a simple sine wave recorded on an ocilloscope. I do not know what I am doing wrong. Any help with what is wrong would be very helpful.
t10k = readtable('Scope_10kHz.csv','NumHeaderLines',11);% 11 skips the first 11 rows of data
T10k = table2cell(t10k); %converts the table to cell
xv = cell2mat(T10k(:,1));
yv = cell2mat(T10k(:,2));
S = [xv,yv];
L = height(xv); % Length of signal
T = xv(L,:)-xv(1,:); % Sampling period
Fs = 1/T; % Sampling frequency
t = (0:L-1)*T; % Time vector
Y = fft(S); %compute the Fast fouier transform
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
figure
plot(S)
xlabel('Time (s)')
ylabel('Voltage (V)')
title('Ocilloscope Measurment')
figure
plot(Y)
xlabel('Frequency')
ylabel('Magnitude')
title('FFT raw')
figure
semilogy(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

Accepted Answer

Star Strider
Star Strider on 27 Sep 2021
It works correctly. However it is also taking the Fourier ttransform of the time vector, so restricting it to only the data vector likely produces the desired result —
t10k = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/750984/Scope_10kHz.csv','NumHeaderLines',11); % 11 skips the first 11 rows of data
T10k = table2cell(t10k); %converts the table to cell
xv = t10k{:,1};
yv = t10k{:,2};
S = [xv,yv];
L = height(T10k); % Length of signal
T = xv(2)-xv(1); % Sampling period
Fs = 1/T % Sampling frequency
Fs = 1.4286e+07
Fn = Fs/2; % Nyquist Frequency
t = (0:L-1)*T; % Time vector
Y = fft(S(:,2)); %compute the Fast fouier transform
P2 = abs(Y/L);
P1 = P2(1:L/2);
P1(2:end-1) = 2*P1(2:end-1);
f = Fn*(0:(L/2)-1)/L;
figure
plot(S)
xlabel('Time (s)')
ylabel('Voltage (V)')
title('Ocilloscope Measurment')
figure
plot(Y)
xlabel('Real')
ylabel('Imaginary')
title('FFT raw')
[pks,locs] = findpeaks(P1, 'MinPeakProminence',0.01)
pks = 2×1
0.1945 0.0523
locs = 2×1
7 13
figure
loglog(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
text(f(locs),pks, compose('Frequency: %.1f Amplitude: %.3f', [f(locs).' pks]), 'Vert','bottom', 'Horiz','left')
.
  2 Comments
Nicholas Calvano
Nicholas Calvano on 27 Sep 2021
That was what I needed! Thank you. I thought it was something simple.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!