How to solve a data length error in the function "filtfilt"?
7 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Star Strider
2021-1-7
From the filtfilt documentation:
x — Input 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.
.
2 个评论
更多回答(1 个)
Mathieu NOE
2021-1-7
hello Lea
your issue is related to the size of the input data
filtfilt works only on "vertical" vectors so the first dimension must be the time
you have to find a way to put the data in 5121x8x30 or 5121x30x8 format
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Digital Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!