How do I compute bandpower?

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);

Answers (0)

Categories

Tags

Asked:

on 30 Mar 2026 at 20:49

Edited:

on 30 Mar 2026 at 23:35

Community Treasure Hunt

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

Start Hunting!