hello everybody, my question what is the median frequency(MF) mat-lab code ,if the median frequency defined as the frequency that divides the magnitude-spectrum in two parts of equal size(the area under the curve for lower frequencies than MF equals the area under the curve for the frequencies higher than MF. first of all i've got the plot of the magnitude spectrum then i used trapz(x,y) to get the whole area under the curve, now i need a code to know what is the frequency that divides the total area into 2 equal parts ?!
Prashant Kumar answered .
2025-11-20
To calculate the median frequency of a signal in MATLAB, you can use the following approach. Median frequency is the frequency at which the power spectrum of the signal is divided into two equal parts.
Here is a sample code to calculate the median frequency:
% Generate a sample signal (replace this with your own signal)
fs = 1000; % Sampling frequency
t = 0:1/fs:1-1/fs; % Time vector
x = cos(2*pi*50*t) + 0.5*randn(size(t)); % Sample signal
% Compute the power spectrum of the signal
[Pxx, F] = pwelch(x, [], [], [], fs);
% Calculate the cumulative power
cumulativePower = cumsum(Pxx) / sum(Pxx);
% Find the median frequency
medianFrequencyIndex = find(cumulativePower >= 0.5, 1);
medianFrequency = F(medianFrequencyIndex);
% Display the median frequency
disp(['Median Frequency: ', num2str(medianFrequency), ' Hz']);
Explanation:
1. Generate Sample Signal: This example generates a sample signal `x` (you can replace it with your own signal).
2. Power Spectrum: The `pwelch` function calculates the power spectral density (`Pxx`) and corresponding frequencies (`F`).
3. Cumulative Power: The cumulative power is calculated using the `cumsum` function.
4. Median Frequency: The median frequency is found where the cumulative power first reaches or exceeds 0.5.
5. Display: Finally, the median frequency is displayed.
This code should help you calculate the median frequency of your signal.