Plotting amplitude spectrum of a signal

i got this sygnal
tmax=0.5;
t=0:0.001:tmax;
fs=1000;
tsamp=0:1/fs:tmax;
f1 = 18;
f2 = 321.37;
y = sin(2*pi*f1*tsamp) + sin(2*pi*f2*tsamp);
figure();
plot(t, y);
grid on;
how do I plot it's amplitude spectrum?

 Respuesta aceptada

Try something like this —
tmax=0.5;
t=0:0.001:tmax;
fs=1000;
tsamp=0:1/fs:tmax;
f1 = 18;
f2 = 321.37;
y = sin(2*pi*f1*tsamp) + sin(2*pi*f2*tsamp);
figure();
plot(t, y);
grid on;
L = numel(t);
Fn = fs/2;
NFFT = 2^nextpow2(L);
FTy = fft(y,NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTy(Iv))*2)
grid on
xlabel('Frequency')
ylabel('Magnitude')
FTyw = fft(y(:).*hann(L),NFFT)/L;
figure
plot(Fv, abs(FTy(Iv))*2)
grid on
xlabel('Frequency')
ylabel('Magnitude')
title('Windowed')
.

4 comentarios

Artyom
Artyom el 8 de En. de 2023
thank you so very much! it looks like what i needed! could you help me a bit more, please? i just got the infromation that the amplitude spectrum should be in dB & in the range of +- half of the sampling frequency. could you modify it that way?
A two-sided Fourier transform is straightforward in MATLAB.
Calculating it with its declared length and with a power-of-2 length and a window —
tmax=0.5;
t=0:0.001:tmax;
fs=1000;
tsamp=0:1/fs:tmax;
f1 = 18;
f2 = 321.37;
y = sin(2*pi*f1*tsamp) + sin(2*pi*f2*tsamp);
figure();
plot(t, y);
grid on;
L = numel(t);
Fn = fs/2;
FTy = fft(y)/L;
Fv = linspace(-fs/2, fs/2, L); % Odd-Length Vector (Symmetric)
figure
plot(Fv, mag2db(abs(FTy)))
grid on
xlabel('Frequency')
ylabel('Magnitude (dB)')
NFFT = 2^nextpow2(L);
FTyw = fft(y(:).*hann(L),NFFT)/L; % Use Power-Of-2 Length & Window
Fv = linspace(-fs/2, fs/2-fs/NFFT, NFFT); % Even-Length Vector (Asymmetric)
figure
plot(Fv, mag2db(abs(FTyw)))
grid on
xlabel('Frequency')
ylabel('Magnitude (dB)')
title('Power-Of-2 & Windowed')
.
Artyom
Artyom el 8 de En. de 2023
it gives me an error in
FTyw = fft(y(:).*hann(L),NFFT)/L;
says "Undefined function 'hann' for input arguments of type 'double'."
could "hann" be replaced somehow?
Star Strider
Star Strider el 8 de En. de 2023
The hann function is part of the Signal Processing Toolbox. Just delete that part of my code, and note that it will still be here if you need it for future reference. (My intent was to demonstrate doing a power-of-2 fft and using a window function.)

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 8 de En. de 2023
Editada: Sulaymon Eshkabilov el 8 de En. de 2023
tmax=0.5;
t=0:0.001:tmax;
fs=1000;
tsamp=0:1/fs:tmax;
f1 = 18;
f2 = 321.37;
y = sin(2*pi*f1*tsamp) + sin(2*pi*f2*tsamp);
figure();
plot(t, y);
grid on;
Y =movmedian(y, 5); % Amplitude
figure
plot(t,Y)
grid on
xlabel('x')
ylabel('|y(x)|')

Productos

Preguntada:

el 8 de En. de 2023

Comentada:

el 8 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by