Plotting amplitude spectrum of a signal

201 ビュー (過去 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');
  2 件のコメント
Ian Cobb
Ian Cobb 2023 年 2 月 10 日
shouldnt the x axis of the second plot be frequency in Hz?
Paul
Paul 2023 年 2 月 10 日
Yes, it should

サインインしてコメントする。


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 Center および File ExchangeSpectral Measurements についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by