1
votes

I am trying to create a band-pass filter within the range of 0.1 Hz to 50 Hz. I am using the filterbuilder tool from MATLAB and so the code it outputs looks like this:

function y = filter050(x)

persistent Hd;

if isempty(Hd)

    Fstop1 = 0.1;    % First Stopband Frequency
    Fpass1 = 0.15;    % First Passband Frequency
    Fpass2 = 45;   % Second Passband Frequency
    Fstop2 = 50;   % Second Stopband Frequency
    Astop1 = 60;   % First Stopband Attenuation (dB)
    Apass  = 1;    % Passband Ripple (dB)
    Astop2 = 60;   % Second Stopband Attenuation (dB)
    Fs     = 500;  % Sampling Frequency

    h = fdesign.bandpass('fst1,fp1,fp2,fst2,ast1,ap,ast2', Fstop1, Fpass1, ...
        Fpass2, Fstop2, Astop1, Apass, Astop2, Fs);

    Hd = design(h, 'equiripple', ...
        'MinOrder', 'any');


    set(Hd,'PersistentMemory',true);

end

y = filter(Hd,x);

The problem is that this runs forever. I understand that my filter will require a high number of points since it is such low frequency and with a sampling rate of 500 Hz, but I really did not want to downsample my signal... Is there any way around it?

Also, I tried downsampling to check if it would run faster, downsampled to 100 Hz and still runs forever.

2
"The problem is that this runs forever." but do you get an output sometime after "forever"? Or does it really run forever "forever"?Robert Seifert
Maybe you are asking too much, and the size of the filter becomes extremely long, or it runs into numerical problems.Bas Swinckels
@thewaywewalk Well, it ran for about 1 day straight, then I just gave up because it is not a good solution for my problem... Edit: the downsampled version runs in a couple of minutes, I would say around 10, which is not too bad, but I would really like to use the whole data...PL-RL
@BasSwinckels that is what I am worried about, any advice?PL-RL

2 Answers

1
votes

Disclaimer: I am not an expert on digital filters, just a casual user. I also don't have any experience with fdesign.

My wild guess is that fdesign either is trying to design a very high order filter which will take forever to optimize, or that it is running into numerical problems. There must be some rules of thumb for designing filters (I don't know them), but there are probably limits on how large the ratio between the cut-off frequencies and sample frequencies or between transition ranges and sample rate can be. In your case, Fstop1 / Fs = 5000 and even (Fpass1 - Fstop1) / Fs = 10000, which is probably way too much. Some things you can do:

  • Try to relax your filter requirements as much as possible: do you really need such a high attenuation? Can you enlarge the frequency range over which you go from pass to stop-band? Would a simple high-pass filter at 0.5 Hz be enough? Can you tolerate more ripple? Etc, ...

  • Downsample your data. You suppress all frequencies above 45 Hz, so obviously you don't care about them. This means that you can safely downsample to about 100 Hz, which might make any numerical problems a factor 5 less critical. What is your problem with down-sampling? This is pretty easy to do with decimate or so: x_dec = decimate(x, 5).

  • Try to solve your problem in a slightly different way. If you read the manual for decimate, you see that it actually does some low-pass filtering with a cut-off frequency at Fc = 0.8*Fsample/ (2 * downsample_ratio). This is necessary to avoid aliasing problems. (Note the difference between down-sampling, which simply picks 1 sample out of n, and decimating, which does proper anti-alias filtering first.) So if you would decimate by say a factor of 4, you get the low-pass part of your filter with a Fc = 0.8*500 / (2*4) = 50 Hz for free! Then all that is left to do is do some high-pass filtering with Fc = 0.2 Hz or so.

0
votes

I once ran into this problem too. I had some electrophysiology data sampled at 5k Hz, and I tried to low-pass filter it using MATLAB's fdesign. It ran terribly slow, and I had to downsample the data to increase speed.

Later I found a good solution. I extract code of the digital filter from EEGLAB (A MATLAB toolbox to process EEG signals). It is a window-sinc filter, and runs extremely fast (I can filter 10-minute long EEG data sampled at 1000 Hz within 1 minute), and window-sinc filter is extremely good at separating one band of frequency from another (very fast roll-off). Now I can filter my electrophysiology data without Downsam