MATLAB: How to solve a data length error in the function “filtfilt”

bandpass filterbutterworth filterdesignfilterror data lengthfiltered signalfiltfilt

Hello,
I tried to code a Butterworth filter 4th order by using designfilt and then used the function filtfilt to apply it on my 3-dimensional EEG data.
But I'm getting an error, that the Data length must be larger than 12 samples, even though the length is larger…
I think I must be missing something. Can somebody help me?
So the size of the EEG data is "8x5121x30 double" and is called "ssvep0Hz". The passband is set between 5Hz and 20Hz. The sample_rate in this case is 512.
  • This is the code I programmed:
function [filtered_signal] = myfilter(n,f1,f2,ssvepdat,samp_rate)
d = designfilt('bandpassiir', 'FilterOrder', 2*n, 'HalfPowerFrequency1', f1, 'HalfPowerFrequency2', f2, 'DesignMethod', 'butter', 'SampleRate', samp_rate);
filtered_signal = filtfilt(d,ssvepdat);
end
  • And then implemented in the script it looks like that:
filtered_ssvep0Hz = myfilter(2,5,20,ssvep0Hz, sample_rate);
Thank you in advance.

Best Answer

From the filtfilt documentation:
xInput signal
vector | matrix | N-D array
Input signal, specified as a real-valued or complex-valued vector, matrix, or N-D array. x must be finite-valued. filtfilt operates along the first array dimension of x with size greater than 1.
So, the dimension to be filtered has to be the first dimension.
This appears to work:
function [filtered_signal] = myfilter(n,f1,f2,ssvepdat,samp_rate)
d = designfilt('bandpassiir', 'FilterOrder', 2*n, 'HalfPowerFrequency1', f1, 'HalfPowerFrequency2', f2, 'DesignMethod', 'butter', 'SampleRate', samp_rate);
filtered_signal = filtfilt(d,ssvepdat);
end
sample_rate = 512;
ssvep0Hz = randn(8,5121,30);
ssvep0Hz = permute(ssvep0Hz,[2 1 3]);
filtered_ssvep0Hz = myfilter(2,5,20,ssvep0Hz, sample_rate);
filtered_ssvep0Hz = permute(filtered_ssvep0Hz, [2 1 3]);
Check = size(filtered_ssvep0Hz)
The first permute call shift the matrix dimensions so filtfilt filters the largest dimension. The second permute call shifts the dimensions back to their original orientations, matching the input matrix.
.