How to plot a single spectrum using FFT from waveform?

clc;
data = csvread('data.csv') ;
t = data(:,1) ;
amp = data(:,2) ;
L = length(t) ;
N = length(amp);
dt = max(diff(t)) ;
Fs = 1/dt ;
%%1st coding
fs=1/2E-5;
t=0:0.0005:1;
fs=1/(t(2));
x3 = sin (2*pi*60*t);
x=amp;
nfft = 1048576;
X=fft(x,nfft);
X=X(1:nfft/2);
mx = abs (X);
f = (0:nfft/2-1)*fs/nfft;
figure,plot(x);
title ('Sine Wave');
xlabel('Time');
ylabel('Frequency');
figure,plot (f,mx);
title ('Power Spectrum of a Sine Wave');
xlabel('Frequency');
ylabel('Power');
%%2nd Coding
Y = fft(amp);
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;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
is there any problem for these coding? any solution?

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by