bandpass filter in multiple frequencies with a for- if- ifelse loop

Hi everyone!
I have to filter a signal in 5 frequency bands, each with its own range and calculate, for each band, an RMS value. I tried to create a loop, but RMS is overwritten every time a new signal is filtered.
the code:
for freq=1:5
if freq==1
freq_min=0,1
freq_max=0,7
fs=1000
[blow,alow]=butter(2,freq_max/(fs/2));
[bhigh,ahigh]=butter(2,freq_min/(fs/2),'high');
filt_signal=filtfilt((blow,alow,signal);
filt_signal=filtfilt(bhigh,ahigh,filt_signal);
RMS_freq1=rms(filt_signal);
elseif freq==2
freq_min=0,7
freq_max=0,9
fs=1000
[blow,alow]=butter(2,freq_max/(fs/2));
[bhigh,ahigh]=butter(2,freq_min/(fs/2),'high');
filt_signal=filtfilt((blow,alow,signal);
filt_signal=filtfilt(bhigh,ahigh,filt_signal);
RMS_freq2=rms(filt_signal);
ecc..
RMS=[RMS_freq1 RMS_freq2 RMS_freq3 RMS_freq4 RMS_freq5];
end
end
each time the script restarts with a new signal, the RMS array is overwritten.

4 Comments

Hi,
First of all, the whole structure of your code needs to be re-written by considering the loop.
Second, you should have included the indexes, e.g.:
freq_min=[0.1; 0.7];
freq_max=[0.7; 0.9];
fs=1e3;
for freq=1:5
[blow,alow]=butter(2,freq_max(freq)/(fs/2));
[bhigh,ahigh]=butter(2,freq_min(freq)/(fs/2),'high');
filt_signal(freq,:)=filtfilt(blow, alow,signal);
filt_signal(freq,:)=filtfilt(bhigh,ahigh,filt_signal(freq,:));
RMS_freq(freq,:)=rms(filt_signal(freq,:));
end
Good luck
sorry, but i don't understand what you mean
You don't need all of these "if .. elseif ... end" conditional statements. A simple [for ... end] loop.
thanks for your help!!
I have 5 frequency bands:
b1: 0.1-0.7 Hz
b2: 0.7-0.9 Hz
b3: 0.9-1.1 Hz
b4 1.1-1.3 Hz
b5: 1.3-1.5 Hz
and i wrote:
freq_min = [0.1; 1.3];
freq_max = [0,7; 1.5];
fs = 1e3;
ecc..
error message: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

Sign in to comment.

Answers (0)

Asked:

on 17 May 2021

Commented:

on 17 May 2021

Community Treasure Hunt

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

Start Hunting!