Spectral analysis in MATLAB script

6 visualizzazioni (ultimi 30 giorni)
Ryan Takatsuka
Ryan Takatsuka il 29 Nov 2018
Risposto: banu cicek il 18 Ago 2020
I am trying to minimize the spectral leakage in the frequency analysis of a signal to obtain accurate magnitudes of all the frequency components. For example, I can create 2 signals: 1 where the signal period is a multiple of the window, and 1 where it is not.
f1 = 10.1; % [Hz] frequency of the first signal
f2 = 10; % [Hz] frequency of the second signal
N = 1000; % length of the signal
Fs = 1000; % [Hz] sampling rate of the signal
data1 = frequency_analysis(f1, Fs, N); % Frequency analysis of the first signal
data2 = frequency_analysis(f2, Fs, N); % Frequency analysis of the second signal
figure
hold on
plot(data1.freq, data1.mag)
plot(data2.freq, data2.mag)
function data = frequency_analysis2(f0, Fs, n)
% d the frequency reponse of the sine wave using another method
time = (0:(n-1)) / Fs;
y = sin(f0*2*pi*time) + randn(1,n)*1e-3;
mag = fft(y);
mag = fftshift(mag);
data.mag = pow2db(abs(mag).^2 / n / Fs) + 30; % convert to dBm
data.freq = (-n/2 : (n-1)/2) * Fs;
data.y = y;
data.time = time;
end
untitled.jpg
However, when I perform the following test in Simulink (attached file) using the Spectrum Analyzer, I get the following response (both the 10.1 Hz signal and the 10 Hz signal show no spectral leakage):
Capture.JPG
How do I perform a frequency analysis fo a signal in MATLAB to produce a result like the one in the Simulink Spectrum Analyzer? I found that the Spectrum Analyzer uses the filter bank method, but I am not sure how to implement that in MATLAB.

Risposte (2)

Brian Hannan
Brian Hannan il 5 Dic 2018
It looks like the difference is that the Simulink model's Spectrum Analyzer is showing the spectrum of the last 1000 samples. The MATLAB script, however, is calculating the spectrum from the entire signal all at once.
If you want to recreate the Simulink result in MATLAB, you'll need to create a for loop that replicates the time-stepping that is happening in Simulink by iterating over a time vector t = 1:1e-3:10 and taking the spectrum of just the last 1000 samples at each time step.

banu cicek
banu cicek il 18 Ago 2020
I'll ask why did you add 30 at this line;
data.mag = pow2db(abs(mag).^2 / n / Fs) + 30; % convert to dBm
I'm trying to do spectrum analyzer with a sensor data but I have offset 30 dBm when I compare my analyzer with spectrum analyzer in simulink.

Community Treasure Hunt

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

Start Hunting!

Translated by