Calculation of mean signal frequency

44 views (last 30 days)
Hi everyone,
I'm trying to perform an analysis in the frequency domain, but I'm confused regarding how to obtain the PSD signal.
Currently, I'm using the following to obtain the PSD:
L = length(data);
% Compute Fast Fourier Transform
Y = fft(data);
% Compute the two-sided spectrum P2
P2 = abs(Y/L);
% Compute the single-sided spectrum P1 based on P2
P1 = P2(1:ceil(L/2)+1);
P1(2:end-1) = 2*P1(2:end-1);
power = P1;
% Define the frequency domain axis
frequencies = fs*(0:ceil(L/2))/L;
I have tested this with the following signal:
fs = 200;
x = 0:1/fs:20
data = sin(2*pi*5*x);
And I get the following PSD:
I'm trying to calculate the mean frequency using the following formula:
mean_frequency = sum(power.*frequencies)/sum(frequencies)
Where I obtain
mean_frequency = 5.0587e-04
which is obviously wrong. However, when I use MATLAB's meanfreq function, I get meanfreq(data, fs) = 4.999Hz, which is the expected.
Can please anyone tell me what I'm doing wrong? After this I want to perform a complete analysis in the frequency domain (e.g, median frequency, total power, etc) and I need to make sure my PSD data is correct.
Many thanks for your attention,

Accepted Answer

Star Strider
Star Strider on 27 Jul 2020
There are two small errors.
Correcting them:
P2 = abs(Y/L).^2; % Power = Amplitude^2
mean_frequency = sum(power.*frequencies)/sum(power) % Divide By Sum Of Power
meanfreq =
Star Strider
Star Strider on 27 Jul 2020
Anything I could recommend would be limited only to my experience.
A much better option would be the MathWorks Academia site For Students, and particularly, Books.
(My personal preference for books is Alibris.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by