MATLAB: Butterworth filtfilt and fft/ifft problem

butterbutterworthdigital signal processingfftfilterfiltfiltifftsignal processingstrong motion

I have to filter strong motion data using a bandpass n=4 butterworth filter with cut-off frequencies of 0.01 and 40Hz. I have the original time series x(t) which I included in a txt file below. However I'm new with signal processing and I'm not sure if my filter is right and the fft functions are used correctly. Here is the code I have :
Fs=1/Ts; %200Hz
fn=1/(2*Ts);
fhc=0.8*fn/(Fs/2); %40Hz
flc=0.01/(Fs/2); %0.01Hz
Wn=[flc fhc];
f=(0:Nf-1)*Fs/Nf;
X=fft(x,Nf);
[b,a]=butter(1,Wn,'bandpass');
Y=filtfilt(b,a,X);
y=ifft(Y,'symmetric');
figure()
loglog(f,abs(X*Ts),'b',f,abs(Y3*Ts),'r');
hfvt=fvtool(b,a,'FrequencyScale','log','Fs',Fs);
When I plot the fourier's amplitude spectrum I get a downward shift in the spectrum. I'm not sure why it does that as the magnitudes plot using fvtool in between the cut-off frequencies are equal to 0. See below :
<<
>>
Thanks in advance.

Best Answer

First, you are specifying an order 1 filter, not an order 4 filter.
Second, your unfiltered signal appears only to have an upper frequency limit of 40 Hz, so your filter is only going to affect its lower frequencies. Because of that, I plotter the unfiltered and filtered signals separately, since they overplot each other.
I would do this:
filename = 'acc time series.txt';
fidi = fopen(filename);
D = textscan(fidi, '%s%s');
t = str2double(strrep(D{1}, ',','.')); % Replace Decimal Separator

X = str2double(strrep(D{2}, ',','.')); % Replace Decimal Separator
L = numel(t);
Ts = mean(diff(t));
Fs = 1/Ts;
Fn = Fs/2;
fhc = 40/Fn;
flc = 0.01/Fn;
Wn=[flc fhc];
n = 4;
[b,a] = butter(n,Wn,'bandpass');
Y = filtfilt(b, a, X);
S = [X Y];
FTS = fft(S)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:numel(Fv);
figure
subplot(2,1,1)
plot(Fv, abs(FTS(Iv,1))*2)
grid
set(gca, 'XScale','log')
title('Unfiltered signal')
subplot(2,1,2)
plot(Fv, abs(FTS(Iv,2))*2)
grid
set(gca, 'XScale','log')
title('Filtered signal')
figure
freqz(b, a, 2^14, Fs)
set(subplot(2,1,1), 'XScale','log')
set(subplot(2,1,2), 'XScale','log')
title(subplot(2,1,1),'Filter Bode Plot')
Experiment to get the result you want.