# Zero-Phase Algorithm in Matlab

From this document it's says

1.Filter the Data
2.Flip that
3.Filter again
4.and Flip that again


for zero-phase filter. but when i do this in matlab , i'm compare between filtfilt method and what i did , it isnt equal. what is the problem ?

MATLAB CODE

clc;clear all;
x= 10;
fs= 4000;
my_counter = 1000;
myRawData = emg_healthy(:,2);
myRawData = myRawData(1:my_counter);
myDataRecti = abs(myRawData);
[b,a]= butter(6,x/(fs/2),'low');
filter_y=filtfilt(b,a,myDataRecti);
firstFilter = filter(b,a,myDataRecti);
flip_firstFilter = fliplr(firstFilter);
secondFilter = filter(b,a,flip_firstFilter);
flip_secondFilter = fliplr(secondFilter);
xlabel('Sample Number');ylabel('Genliği');plot(flip_secondFilter,'b');
hold on ;
xlabel('Sample Number');ylabel('Rect Genliği');plot(filter_y,'r');


RESULT

-----------------------------------EDIT-----------------------------------

thanks to robert bristow-johnson, i tried zero-padding to myData , end and begin. This is i try

MATLAB CODE

% % EMG Soleus
clc;clear all;
x= 10;
fs= 4000;
my_counter = 1000;
myRawData = emg_healthy(:,2);
myRawData = myRawData(1:my_counter);
myDataRecti = abs(myRawData);
[b,a]= butter(6,x/(fs/2),'low');
for i = 1:1:(my_counter/4)
myZeroDataRecti(i) = 0;
end
for i = (my_counter/4+1):1:(my_counter+my_counter/4)
myZeroDataRecti(i) = myDataRecti(i-my_counter/4);
end
for i=(my_counter+my_counter/4+1):1:(my_counter+my_counter/4+my_counter/4)
myZeroDataRecti(i) = 0;
end
filter_y=filtfilt(b,a,myZeroDataRecti);
firstFilter = filter(b,a,myZeroDataRecti);
flip_firstFilter = fliplr(firstFilter);
secondFilter = filter(b,a,flip_firstFilter);
flip_secondFilter = fliplr(secondFilter);
xlabel('Sample Number');ylabel('Amplitude');plot(myZeroDataRecti,'g');
hold on ;
xlabel('Sample Number');ylabel('Amplitude');plot(flip_secondFilter,'b');
xlabel('Sample Number');ylabel('Amplitude');plot(filter_y,'r');
%


RESULT

another i try , i again add zero-padding begin and end but i used filtfit method to apply without zero-padding data

MATLAB CODE

% % EMG Soleus
clc;clear all;
x= 10;
fs= 4000;
my_counter = 1000;
myRawData = emg_healthy(:,2);
myRawData = myRawData(1:my_counter);
myDataRecti = abs(myRawData);

[b,a]= butter(6,x/(fs/2),'low');
filter_y=filtfilt(b,a,myDataRecti);
for i = 1:1:(my_counter/4)
myZeroDataRecti(i) = 0;
end
for i = (my_counter/4+1):1:(my_counter+my_counter/4)
myZeroDataRecti(i) = myDataRecti(i-my_counter/4);
end
for i=(my_counter+my_counter/4+1):1:(my_counter+my_counter/4+my_counter/4)
myZeroDataRecti(i) = 0;
end
firstFilter = filter(b,a,myZeroDataRecti);
flip_firstFilter = fliplr(firstFilter);
secondFilter = filter(b,a,flip_firstFilter);
flip_secondFilter = fliplr(secondFilter);
xlabel('Sample Number');ylabel('Amplitude');plot(flip_secondFilter,'b');
hold on ;
xlabel('Sample Number');ylabel('Amplitude');plot(myDataRecti,'g');
xlabel('Sample Number');ylabel('Amplitude');plot(myZeroDataRecti,'c');
xlabel('Sample Number');ylabel('Amplitude');plot(filter_y,'r');
%


RESULT

in my opion, there are two things we have to think about that,

1.First picture in the edit part,my signal (myCode-LEGEND) amortize itself , but matlab function (filtfilt) keep continiues , why is that ?

2.Second picture in the edit part, my signal(myCode-LEGEND) different from filtfilt function signal. And between myCode and 250Zero+originalData+250Zero i think it doesnt any phase shift. If we say that it's true what should i do ? Will i remove 250 zero from begin and end ?

• looks to me like an offset issue. try zero-padding the initial data on both ends (long enough to get most of the "tails" of the impulse response in there) and do it with filtfilt() and manually as you have. in the meantime i am going to try to repeat your experiment and see what i get. – robert bristow-johnson Dec 30 '15 at 23:42
• You're still asking yourself why you get a result that's different from filtfilt. Again, as I said in my answer, it's the initial conditions for both calls to filter that make the difference. Zero-padding might get you closer to filtfilt but you will not get the same result. – Matt L. Dec 31 '15 at 11:19
• İ understand but matlab is just a program it isnt drop from sky, someone make this algorithm and write in matlab, and i want to know how – massaimara98 Dec 31 '15 at 11:36
• That's why I gave you the reference to the relevant paper. If you want to understand things you might need to do some reading, just like the person who wrote filtfilt. – Matt L. Dec 31 '15 at 13:02

As far as I can see you're doing the basic filtering right. The difference with Matlab's filtfilt function is that the latter uses certain initial conditions in the calls to filter to minimize transients.
filtfilt minimizes start-up and ending transients by matching initial conditions, and you can use it for both real and complex inputs