How do I compute bandpower?
Show older comments
Which parts of this code is correct? I'm getting confused on which sections are relevant for my question above?
N = length(sig1);
xdft1 = fft(sig1);
xdft1 = xdft1(1:N/2+1);
psdx1 = (1/(Fs*N)) * abs(xdft1).^2;
psdx1(2:end-1) = 2*psdx1(2:end-1);
freq1 = 0:Fs/length(sig1):Fs/2;
P1_band = psdx1(freq1 >= 20 & freq1 <= 100);
f1_band = freq1(freq1 >= 20 & freq1 <= 100);
power_sig1 = trapz(f1_band, P1_band);
noise = x_new - x_notch;
SNR_dB = snr(x_notch, noise);
P_signal = mean(signalSeg.^2, 'omitnan');
P_noise = mean(noiseSeg.^2, 'omitnan');
SNR_dB = 10*log10(P_signal / P_noise);
f_notch = 60;
BW_notch = 2;
wo = f_notch/(Fs/2);
Q = f_notch/BW_notch;
bw = wo/Q;
[b_n,a_n] = iirnotch(wo,bw);
x_notch = filtfilt(b_n,a_n,x_bp);
x = x(:);
t = (0:length(x)-1)'/Fs;
% Frequency analysis
x_dm = x - mean(x);
N = length(x_dm);
X = fft(x_dm);
K = floor(N/2);
X = X(1:K+1);
PSD = (1/(Fs*N)) * abs(X).^2;
if K > 1
PSD(2:end-1) = 2*PSD(2:end-1);
end
f = (0:K)' * (Fs/N);
% Find dominant frequency
[~, idxPeak] = max(PSD(2:end)); % ignore 0 Hz
idxPeak = idxPeak + 1;
domFreq = f(idxPeak);
fprintf('Dominant frequency = %.2f Hz\n', domFreq);
1 Comment
Mathieu NOE
42 minutes ago
this looks like a concatenation of several scripts , we have multiple sections doing different things, and different signals / variables (sig1,x , x_bf)
I don't know what the notch filter was supposed to do , it's not clear to me
if the question is about bandpower you have the code example already here (looks pretty much what the 1st section is doing) : Power Spectral Density Estimates Using FFT - MATLAB & Simulink
with a recent signal processing toolbox you can get yourresult in one line : bandpower - Band power - MATLAB
all the best
Answers (0)
Categories
Find more on Spectral Measurements 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!