How to Calculate dominant frequency and centroid frequency and plot them together

14 views (last 30 days)
please help me, I would like to calculate dominant and centroid frequency and plot them tohether in one plot. Please CMIIW if i have misundertanding the basic concept. As I undertand, dominant freq is higest freq that appear in our signal. I used FFT and get the max power. And about dominant freq I am not quite sure. What makes me don't understand is, if I take example from weighted mean as basic of centroid freq, based on example in https://en.wikipedia.org/wiki/Weighted_arithmetic_mean, after calculate weighted mean we have weighted mean of the frequencies present in the signal, meaning that is not a single value. I use spectralCentroid to calculate centroid frequency, but then i confuse, if i have single falue for dominant freq and several value from spectralCentroid, how i should plot them together?
i also attach my file, basically the code will doing looping as my file represent one day data, so my goal is plot dominant and centroid freq in one plot for 1 month. I assume my dominant freq has 30 data point, but not sure about spectralCentroid.
this is my attempt to do it.
sig=load('signal2023-335.txt');
%BAND PASS FILTER long range --> can be considered as UNFILTER condition
% Define the parameters
lowcut = 1/400; % Low cutoff frequency in Hz
high1 = 40; % High cutoff frequency in Hz
high2 = 1.25; % Midrange cutoff frequency in Hz
fs = 100; % Sampling frequency in Hz
% Design the Butterworth bandpass filter (UNFILTER)
[bU, aU] = butter(2,[lowcut high1] / (fs / 2), 'bandpass');
sigUnfilt= filtfilt(bU,aU,sig);
% calculate FFT UNFILT
n=length(sigUnfilt);
srate=100; %sampling rate
nyq=srate/2;
fftsig=fft(sigUnfilt-mean(sigUnfilt))/n; %dataX is fft result from filtered signal
hz=linspace(0,nyq,floor(n/2)+1);
pow=2*abs(fftsig(1:length(hz)));
Iv = 1:numel(hz);
%get the maximum freq/dominant freq
[~,idx] = max(pow) ;
domfreq = hz(idx)
% Calculate centroid frequency
centroid = spectralCentroid(sigUnfilt,100)

Answers (1)

Star Strider
Star Strider on 16 Jan 2025
The spectralCentroid 9s plotted with respect to time, and the values go from near zero to 7.14 Hz. The dominant frequency is 0.0042 Hz. You can plot both of them in the same axes, however the dominant frequency barely shows up, being near zero.
sig=load('signal2023-335.txt');
%BAND PASS FILTER long range --> can be considered as UNFILTER condition
% Define the parameters
lowcut = 1/400; % Low cutoff frequency in Hz
high1 = 40; % High cutoff frequency in Hz
high2 = 1.25; % Midrange cutoff frequency in Hz
fs = 100; % Sampling frequency in Hz
% Design the Butterworth bandpass filter (UNFILTER)
[bU, aU] = butter(2,[lowcut high1] / (fs / 2), 'bandpass');
sigUnfilt= filtfilt(bU,aU,sig);
figure
freqz(bU, aU, 2^16, fs)
set(subplot(2,1,1), 'XScale','log')
set(subplot(2,1,2), 'XScale','log')
% calculate FFT UNFILT
n=length(sigUnfilt);
srate=100; %sampling rate
nyq=srate/2;
fftsig=fft(sigUnfilt-mean(sigUnfilt))/n; %dataX is fft result from filtered signal
hz=linspace(0,nyq,floor(n/2)+1);
pow=2*abs(fftsig(1:length(hz)));
Iv = 1:numel(hz);
figure
plot(hz, pow)
xlim([0 2.5])
%get the maximum freq/dominant freq
[~,idx] = max(pow) ;
domfreq = hz(idx)
domfreq = 0.0042
% Calculate centroid frequency
centroid = spectralCentroid(sigUnfilt,100)
centroid = 3749×1
0.0000 0.0001 0.0005 0.0002 0.0002 0.0014 0.0013 0.0008 0.0027 0.0011
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[centlo,centhi] = bounds(centroid)
centlo = 3.1231e-05
centhi = 7.1436
t = linspace(0, numel(sig)-1, numel(sig)).'/fs;
tsc = linspace(0, 1, numel(centroid)).'*max(t);
figure
tiledlayout(2,1)
nexttile
plot(t, sig)
grid
title('Signal')
nexttile
plot(tsc, centroid)
yline(domfreq, '-r', "Dominant Frequency = "+domfreq, LineWidth=1.5)
grid
title('Centroid)')
.
  2 Comments
Star Strider
Star Strider on 17 Jan 2025
That is the way spectralCentroid calculates it. It windows the signal and calculates the centroid of each winidow. (I do not have the Audio Toolbox, so your question is the first time I encountered it.) The signal Processing Toolbox has the meanfreq and medfreq functions, that do produce scalar results. Also consideer the powerbw function, depending on what you want to do.

Sign in to comment.

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!