Showing results for 
Search instead for 
Did you mean: 

Filtering a Square Wave

I am having a filtering problem and I'm going to have to chalk it up to me not understanding filtering well enough.  I am sampling a digital signal which is producing a pretty decent square wave.  The circuit has an RC filter.  However, the environment is still causing a bit of sinusoidal noise.  


I thought to myself, a LowPass filter in my code will surely resolve this.  The digital signal produces square waves at a rate of 1500 peaks per second.  The sinusoidal oscillation is a little over twice the rate of the square wave, so I set my lowpass filter to 1600 hz.  (this is all done in LabVIEW btw)  


No results.  Changed it to 1600hz.  No results.  Used a bandpass filter between 1300 hz and 1600 hz.  No results.  Am I doing something wrong?


 Is it not feesible to filter a square wave to make a better square wave?  I'd give you the code, but there's no point without the data.  I'll be able to make that available tomorrow if it's really necessary.

0 Kudos
Message 1 of 24

Just digitize it.  Compare the values to a threshold. T-F is all you need.

"Should be" isn't "Is" -Jay
0 Kudos
Message 2 of 24

Oops... that's my bad.  I probably should have specified why it even matters.  I am trying to measure the rise and fall time.  The noisy sinusoidal wave is messing with the 10%-90%.

0 Kudos
Message 3 of 24

Thanks.  Now that we have that requirement, sample data and sample code would help us to help you.

"Should be" isn't "Is" -Jay
0 Kudos
Message 4 of 24

I agree that we need to see some data and code.


In general terms a filter will not do what you want and most likely will destroy the rise time information you are trying to measure. Consider that a pure square wave has all odd harmonics out to infinity and that the higher harmonics are what makes the rising edge "square." So applying a low pass filter to a square wave will likely modify the rise time by removing some of the higher harmonics.


Do you know the source of the interfering signal? Is its frequency constant or does it vary? How much noise and other interference is on the desired signal?



Message 5 of 24

I've attached a VI (LabVIEW 2012) that has the data as a constant and the filter VI that I'm using.  The graph can be zoomed in to get a better look.

0 Kudos
Message 6 of 24

Hi Dose,


This is how your data look:


(I applied a low pass using 777Hz with a FIR filter with 29 taps.)


What's your problem to find the 10%/90% points?

Low level is ~1.9V, high level at ~3.8V making 1.9V between those levels. The 10% level is at ~2.1V, 90% level ~3.6V. At both levels I cannot see problems to detect them with high confidence…


When this "sine shaped" noise with an amplitude of ~0.15V (pk-pk) is a problem for you: this seems to be a harmonics of the square wave. Add a sine wave with suitable frequency/phase/amplitude to cancel out most of this noise…

Best regards,

using LV2011SP1 + LV2017 (+LV2020 sometimes) on Win10+cRIO
Message 7 of 24

In the book "Noise Reduction Techniques in Electronic Systems" (by Ott) I learned that there is a proggresion we can work through to elliminate noise. It start at thenoise source and then moves to the next best palce to fix the noise. Working from Memory it goes...


1) Fix the noise source.

2) Confine the noise to the source

3) Move the source of the noise away from affected system

4) Prevent the noise from getting in

5) Remove the noise from the signal. 


Expaning on thos ideas a bit;


1) Fix the noise source.

If a motor has bad brushes with alot of sparking and arcing, fix the brushes.


2) Confine the noise to the source

If you replace the brushes and you stil have noise, put the motor inside a shielded enclosure to keep the noise signal confined the enclosure. Old banks of TV monitors used to use "mu-metal" to prevent the signals used to stear the electron beam of one monitoring interfering with a monitor beside it.


3) Move the source of the noise away from affected system

Move the measurment system away from the noise source. Keep signal wires away from power lines. I once had a funky signal that I traced down to the distribution panel for the building being located on the other side of the wall from where my bench was located. Moved the PC to the end of the bench, problem solved.


4) Prevent the noise from getting in

Use ferite beads on wires entering enclosures. Use shield enclosures. Only allow wires to cross each other at right angles to each other.


5) Remove the noise from the signal.

Filter it out, or attempt to issolate the noise signal and use an inverted signal to cancel the noise in the signal of interest.


For the data you showed in your VI, it appears the noise is constant adn is superimposed on y the signal you are trying to analyze.


A) Can you locate the source of the noise and fix it?

B) Can you shield the noise source?

C) Can you move away from the noise source?

D) Are you using the correct type of wiring, shield, twisted pair ferite beads?...?

E) Only after you have attempted all of the other dieas, try to remove noise from the signal.


That is the best I can offer at the moment.


I do strongly recomend the book by Ott. He starts out with a problem situation, applies Maxwell's law's, makes some assumptions about boundry conditions and then reduces the problem to straight froward rules of thumb. If anyone is serious about making good quality low noise measurements, Ott is a must read.




Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 24

Addition to my last post:


Here you see why it's a bad idea to apply filters to your signal: the edges of the "square" become less steep! (red=original, blue=filtered as described above)

Best regards,

using LV2011SP1 + LV2017 (+LV2020 sometimes) on Win10+cRIO
Message 9 of 24

I was using the available NI Transition measurements to measure the rise and fall times.  But I'm pretty sure the noise is causing the trough and peaks to be higher and lower than desired which is causing rise and fall times to be longer than desired.  Should I make something custom to measure the "peak of a trough" (I know, weird way to say that) and the trough of a peak (again... I know) and then base my 10/90 off of that? 


In terms of reducing noise.  That's out of my power.  I'd design the system differently if I were in charge but alas.... lowly programmer.

0 Kudos
Message 10 of 24