How to solve a data length error in the function "filtfilt"?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Star Strider
le 7 Jan 2021
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 commentaires
Plus de réponses (1)
Mathieu NOE
le 7 Jan 2021
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 commentaires
Voir également
Catégories
En savoir plus sur Digital Filtering dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!