what is the median frequency matlab code?

haya alawneh · Oct 11, 2022 · 2.1K views
Question
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 ?!
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Sep 12, 2026

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. 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!