signal does not show filtered, using filter()

Hello,
I have a signal that I must filter out 60-Hz noise from. I've setup the three moving averages that I want to use to see which one will filter out the 60-Hz noise. However, when I use the coefficients with the unfiltered signal using the filter() function, the plot of the unfiltered signal and the "filtered" signal remains the same. Any advice would be helpful. If someone wants to also use the text file that contains the data for the unfiltered signal, that is attached as well.
%Moving Averages' Coefficients
ync = [.25, .5, .25];
ync1 = [-.085 .342 .485 .342 -.085];
ync2 = [-.090 .060 .168 .233 .255 .233 .168 .060 -.090]
Fs1 = 500; %Sampling Frequency
Ts1 = 1/Fs1; %Sampling Time
t1 = 0:1/Fs1:1-1/Fs1;
data = load("data.txt");
dataMean = mean(data, 2);
plot(t1,dataMean); %Unfiltered Signal
z = filter(ync,1,dataMean); %Choice of Moving Average to test for filtered results
plot(t1,z);
The issue is that the unfiltered signal from the data.txt plot(t1,dataMean) looks identical to the intended filtered signal z = filter(ync,1,dataMean).

 Accepted Answer

Use freqz to see what the filters are actually doing —
Fs1 = 500; %Sampling Frequency
ync = [.25, .5, .25];
ync1 = [-.085 .342 .485 .342 -.085];
ync2 = [-.090 .060 .168 .233 .255 .233 .168 .060 -.090];
figure
freqz(ync,1, 2^16, Fs1)
sgtitle('ync')
figure
freqz(ync1,1, 2^16, Fs1)
sgtitle('ync_1')
figure
freqz(ync2,1, 2^16, Fs1)
sgtitle('ync_2')
So none of them are going to filter out the 60 Hz signal.
To design one that specifically will do that —
Fs1 = 500; %Sampling Frequency
freqs = [57 59 61 63];
mags = [1 0 1];
devs = [0.05 0.01 0.05];
[n,Wn,beta,ftype] = kaiserord(freqs,mags,devs,Fs1)
n = 560
Wn = 2×1
0.2320 0.2480
beta = 3.3953
ftype = 'stop'
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
figure
freqz(hh, 1, 2^16, Fs1)
sgtitle('60 Hz Notch Filter')
This is a relatively long filter, however it should do what you want.
Use the filtfilt function to do the actual filtering.
.

4 Comments

@Star Strider, you've helped me before on a similar issue, so thank you for responding to this one. However, I guess I should've stated that I must use one of the moving averages (ync, ync1, or ync2). So, which one may suppress the 60-Hz noise the best (not necessarily eliminate it)? But you're saying none of them, according to the plots presented, will change anything to the unfiltered signal? Will it matter if I take the plot of the dataMean and plotted it across 5ms?
This is my unfiltered signal across one second.
All FIR filters can be considered in some senseto be ‘moving average’ filters.
All three filters will do some filtering, however it appears that ‘ync2’ will reduce the 60 Hz noise the most, according to the freqz plots.
It is relatively straightforward to design a simple FIR filter to specifically filter out the 60 Hz signal —
Fs1 = 500; %Sampling Frequency
h = poly(exp([-5:5]*1j*2*pi*60/Fs1))
h = 1×12
1.0000 2.2936 1.9694 0.5413 -0.0680 0.0344 -0.0344 0.0680 -0.5413 -1.9694 -2.2936 -1.0000
figure
freqz(h, numel(h)/2, 2^16, 500)
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/612055/data.txt')
T1 = 500×5 table
Var1 Var2 Var3 Var4 Var5 _________ ________ _________ _________ ________ 1.6067 1.343 0.86572 1.4553 1.4346 1.1257 1.2429 0.55563 1.1171 0.9255 0.31181 0.57671 0.027391 0.45708 0.12871 -0.34201 -0.57517 -0.56052 -0.007544 -0.50925 -0.8969 -0.90056 -0.78215 -0.44035 -0.71257 -0.87982 -0.54901 -0.62347 -0.24017 -0.53436 -0.089725 0.21301 -0.081181 0.49866 0.28625 1.1962 1.0583 0.42841 1.1559 1.2145 1.8389 1.7791 0.90144 1.4227 1.7852 1.6258 1.9163 0.91537 1.1888 1.8077 1.0158 1.5151 0.12714 0.40545 1.1428 0.63252 0.81812 -0.4624 -0.42822 0.57762 0.27022 0.17622 -0.6185 -1.1129 0.15782 0.32022 0.13102 -0.55014 -1.2411 0.20912 1.1867 0.85112 -0.25976 -0.7688 0.57522 1.9521 1.5663 0.36639 -0.10602 1.0207
signal = T1{:,1};
L = numel(signal)
L = 500
t = linspace(0, L-1, L)/Fs1;
signal_filtered = filtfilt(h, numel(h)/2, signal);
figure
subplot(2,1,1)
plot(t, signal)
grid
title('Unfiltered Signal')
subplot(2,1,2)
plot(t, signal_filtered)
grid
title('Filtered Signal')
Comparing it to the more sophisticated FIR filter —
freqs = [50 58 62 70];
mags = [1 0 1];
devs = [0.05 0.01 0.05];
[n,Wn,beta,ftype] = kaiserord(freqs,mags,devs,Fs1)
n = 140
Wn = 2×1
0.2160 0.2640
beta = 3.3953
ftype = 'stop'
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale');
signal_filtered_2 = filtfilt(hh, 1, signal);
figure
subplot(2,1,1)
plot(t, signal)
grid
title('Unfiltered Signal')
subplot(2,1,2)
plot(t, signal_filtered_2)
grid
title('Filtered Signal 2')
The 60 Hz interference is relatively high, and while definitely unsophisticated, the simple filter gives a reasonably good account of itself.
.
So how can I get the results of subplot(2,1,2) only using the filter function and the ync2 (since you've stated that this is the best out of the three) coefficients, if possible? I agree, your method is more sophisticated. But I'm restricted to using only the filter function and a choice of the three created moving averages (ync, ync1, or ync2); I can't use filtfilt.
I would just do something like this —
signal_filtered = filter(ycn2, 1, signal)
That will not eliminate the 60 Hz noise because the filter is not designed to do that. It will simply attenuate it a bit.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!