# How to filter my noisy square wave so that i can recover my signal using the Chebyshev Filter

I have a noisy square signal as input but it has a lot of noise. I should use a Bandpass filter to recover my signal.

I know that the Chebyshev Filter is a bandpass filter; but it doesn't work. the data is still noisy after filtering.

Chebyshev Filter: The Chebyshev filter gives a sharper cutoff than a Butterworth filter in the pass band. A disadvantage of the Chebyshev filter is the exterior of gain minima and maxima below the cutoff frequency. Due to the ripples in the pass-band, it is not used in the audio systems. Though it is far better in some applications where there is only one frequency available in the pass band, but numerous other frequencies are required to eliminate.

 % I use A picoscope2000 serie for data acquisition. (MATLAB)
ps2000_getdata;

% The data is saved a cha_a (MATLAB)
cha_a = (bufferChA/1000);

% Here i use the Chebyshev bandpass filter to filter the noisy data(MATLAB)
[A,B,C,D] = cheby2(10,40,[900 1100]/1500);
d = designfilt('bandpassiir','FilterOrder',20, ...
'StopbandFrequency1',900,'StopbandFrequency2',1100, ...
'StopbandAttenuation',40,'SampleRate',8000);

y= filter(d,cha_a); % convolution

sos = ss2sos(A,B,C,D);
fvt = fvtool(d,y,'Fs',8000);
legend(fvt,'cheby2','designfilt')


This is the result of the magnitude response :

And this is the impulse response :

My input data is suppose to be a squared data. And when you look at the impulse response of the output, it does not look like the impulse response of a square signal. There is stil noise. How can i adjust my filter ? Or did i used the filter well ?

## migrated from engineering.stackexchange.comJan 4 at 13:16

This question came from our site for professionals and students of engineering.

• It would be helpful to know why it does not work because there is indeed a infinite way of making the filters. There is no guarantee that even after listing all types that this solves your problem. – joojaa Jan 2 at 14:48
• To help you rephrase - you should describe the system in use, what general reasons or needs you have to filter out various parts of the spectrum, and what the desired output is. – Carl Witthoft Jan 2 at 16:35
• You haven't specified several relevant factors: is your signal bi-level or does the amplitude vary? What's your relevant frequency range, Hz. or GigaHz? Those characteristics will significantly constrain your signal-conditioning options. Note that linear filtering isn't your only choice here! – Catalyst Jan 2 at 16:43
• I'm completely confused as to what you are trying to do here... the comment in your code say "butterworth" filter, but then you design a "cheby2" filter, which is a different thing, and then you don't even use the cheby2 filter at all, you just throw that away and then directly design a digital IIR filter. Which of these three completely different types of filters are you really trying to ask about? – Daniel Kiracofe Jan 5 at 17:36
• @DanielKiracofe, i dont know how to use the cheby2 filter. I will be glad if you can help with an example. I was thinking that d = designfilt(); was part of the same filter. I took the code from here : mathworks.com/help/signal/ref/butter.html – Valona Jan 29 at 12:09

this may not answer your original question, but it answers your comment, and my response would be way too long for a comment.

I think you are confusing multiple different concepts. There are two steps to filtering in matlab, first you design a filter, then you use the filter. The line:

[A,B,C,D] = cheby2(10,40,[900 1100]/1500);


is an example of one possible way to design a filter. This example is a Chebyshev type 2 filter. It uses a 10 pole pair (i.e. 20th order) bandpass filter, with 40 dB of attenuation in the stop band, a sampling frequency of 2*1500 = 3000 Samples/s, and a cuttoff frequency range of 900 - 1100 Hz.

The line:

d = designfilt('bandpassiir','FilterOrder',20, ...
'StopbandFrequency1',900,'StopbandFrequency2',1100, ...
'StopbandAttenuation',40,'SampleRate',8000);


is a completely different way to design a filter. This is not necessarily a Chebyshev filter, or really any of the "classic" filter. You just give matlab the parameters or constraints you care about, and it designs the best filter it can given those parameters.

Now, in your code, you've designed two different filters. That's okay if you want to compare the performance of different filters. i.e. try this one and see if it is better or worse than that one. But I suspect at this point you really just need one or the other, you probably don't need both.

Your next step is to use the filter. That's this line:

  y= filter(d,cha_a); % convolution


The thing you did not do, which you probably should do, is to plot the filtered data and compare it back to your original filter. you can do this in either time domain or frequency domain. I'd start in time domain. i.e. do something like this

figure;
plot( 1:length(y), cha_a, 1:length(y), y);


hopefully this gives you some more insight into how matlab filters work, and then maybe you can ask more focused questions on your specific problem.

Bandpass filtering won't do what you're after. If I take a noiseless square wave with the spectrum in red, and design a bandpass filter for it (response in blue):

and then apply this filter to the original (light blue) square wave, then I get the dark blue response.

I suspect that a better approach might be to use a median filter.

If I apply some gaussian noise to the square wave (blue line), and then apply a median filter (orange plot) then the result is below.

These plots were generated in python; I don't have easy access to Matlab. Code below.

from scipy import signal
import math
import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,20*math.pi,1000)
sq = scipy.signal.square(t)

b, a = signal.cheby1(4, 0.1, [0.01, 0.2], 'bandpass')

plt.rcParams['figure.figsize'] = [20, 10]
plt.figure(1)
w, h = signal.freqz(b, a)
w2, h2 = signal.freqz(sq, 1)
plt.plot(w, abs(h)*600)
plt.plot(w2, abs(h2), color='red')

plt.figure(2)
plt.rcParams['figure.figsize'] = [20, 10]
plt.plot(t,sq)
sq_f = scipy.signal.lfilter(b,a,sq)
plt.plot(t,sq_f,color='blue')

sq_noisy = sq + np.random.normal(0,1,1000)
plt.figure(3)
plt.plot(t,sq_noisy)
sq_noisy_f = scipy.signal.medfilt(sq_noisy,21)
plt.plot(t,sq_noisy_f,color='orange')

• I think the distortion of the waveform comes just from the phase response of the filter. A linear-phase filter or a forward-backward Chebyshev filter should work quite well. But I'm not sure @Valona even has a square wave signal. – Olli Niemitalo Jan 30 at 6:37
• Agreed. The comments seem to say something else. Oh well. And I've lost the tick. :-) – Peter K. Jan 30 at 15:09

You want to use a device called a Schmitt Trigger to filter out the noise from your square wave. I will refrain from having to give you a tutorial on Schmitt Triggers, if you want to have a career in electronics you will need to master the art and science of signal conditioning. There is plenty of information about this online.