signal does not show filtered, using filter()

1 view (last 30 days)
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

Star Strider
Star Strider on 9 May 2021
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
Terry Carney
Terry Carney on 9 May 2021
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.
Star Strider
Star Strider on 9 May 2021
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!