Plotting amplitude spectrum of a signal

212 次查看(过去 30 天)
prodeje
prodeje 2020-4-23
回答: Sady 2023-10-25
So I need to generate a segment of 95 Hz sine wave for the duration of 35 ms, with 3.5 kHz sampling rate and display it in 2 graphs, time domain and amplitude spectrum.
I am struggling with amplitude spectrum, I imagine function fft is required, but how should it be used in this case?
Thanks in advance!
Here is my code below for representation in time domain:
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');

回答(3 个)

Ameer Hamza
Ameer Hamza 2020-4-23
编辑:Ameer Hamza 2020-4-23
Try the following code. Also see the first example on the documentation page of ff() for more details.
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
subplot(2,1,1);
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');
subplot(2,1,2);
f = Fs*linspace(0, 1/2, floor(numel(x)/2));
fr = fft(x);
fr = abs(fr)/numel(x);
fr = fr(1:floor(end/2));
fr(2:end-1) = 2*fr(2:end-1);
plot(f, fr);
xlabel('Time(S)');
title('Frequency Response');

Mehmed Saad
Mehmed Saad 2020-4-23

Sady
Sady 2023-10-25
Fs = 3500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.35; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 95; % hertz
x = cos(2*pi*Fc*t);
% Plot the signal versus time:
subplot(2,1,1);
plot(t,x);
xlabel('Time(S)');
title('Signal versus Time');
subplot(2,1,2);
f = Fs*linspace(0, 1/2, floor(numel(x)/2));
fr = fft(x);
fr = abs(fr)/numel(x);
fr = fr(1:floor(end/2));
fr(2:end-1) = 2*fr(2:end-1);
plot(f, fr);
xlabel('Time(S)');
title('Frequency Response');

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by