问题
new attempt to understand this.
I'm trying to get what bandpass
matlab function does.
So digging into the m-files, the filter object is constructed with:
opts=signal.internal.filteringfcns.parseAndValidateInputs(x,'bandpass',var);
opts = designFilter(opts);
Then, the filtering is done with:
y = signal.internal.filteringfcns.filterData(x,opts);
Then going into the filterData function, the actual filtering is done with:
y = filtfilt(opts.FilterObject,x);
because it is an IIR filter.
Now, if I look into the filtfilt
function, it doesn't have a way to deal with the filter object, only the b-a
, or the sos-g
representation are possible. b-a
representation is really bad, so I tried with the sos-g
representation, and the results are not the same.
I plotted three signals, filtered with: filterData
, filtfilt
with filter object and filtfilt
with sos-g
representation. Only the first two are equal.
Next are the PSD of each signal:
filtData:
filtfilt filter object:
filtfilt sos-g:
Notice the greater psd on the lower frequencies on the sos-g
filter.
So, what does matlab does different when a filter object is used int the filtfilt
function?
Here there is an example (download data.txt):
load('data.txt')
srate=64;
freqrange=[0.4 3.5];
x=data(60*64:64*90);
var{1}=freqrange; var{2}=srate;
%% append at the beginning and at the end m=numel(x);
R=0.1;%10% of signal
Nr=50;
NR=min(round(m*R),Nr);%At most 50 points
x1=2*x(1)-flipud(x(2:NR+1));%maintain continuity in level and slope
x2=2*x(end)-flipud(x(end-NR:end-1));
x=[x1;x;x2];
%% design filter
opts=signal.internal.filteringfcns.parseAndValidateInputs(x,'bandpass',var);
opts = designFilter(opts);
%% default filtering
xx = signal.internal.filteringfcns.filterData(x,opts);
x_fil=xx(NR+1:end-NR);
%% what default filtering is supposed to do
xx = filtfilt(opts.FilterObject,x);
x_fil2=xx(NR+1:end-NR);
[z,p,k]=opts.FilterObject.zpk;
[sos, g] = zp2sos(z, p, k);
%% default filtering with sos-g method
xx = filtfilt(sos,g,x);
x_fil3=xx(NR+1:end-NR);
f=figure;
plot([x_fil x_fil2 x_fil3])
title('default vs ff_D vs ff_{sos}')
legend('filterData','filtfilt_D','filtfilt_{sos}')
grid('on')
来源:https://stackoverflow.com/questions/58958392/comparing-filtered-data3-filtdata-bandpass-vs-filtfilt-with-filter-object-vs