# Why a low-pass filter applied to a square wave gives a weird wave?

When a low-pass filter acts on a square wave, the output is something similar to this: My question is simple. If the filter is a low-pass filter, it should filter all frequencies that are above some constant frequency (the value will depend on the circuit), and let only lower frequencies pass, right?

If this was true, the output should be something like this: In the image, the square wave is truncated by considering terms only up to the fifth harmonic, which is exactly the kind of behavior I would expect to be the output signal of a system that attenuates higher frequencies.

Now, why is the output the first wave, instead of the second wave? Does this happen because the low pass filter just logarithmically attenuates the signal, instead of act like a perfect ideal low-pass filter? (By perfect low pass filter, I mean a filter that would keep exactly the same gain over the signal until a certain frequency, and for higher frequencies would have a zero gain, like in the image below) Thanks

• My father used to say "solve the differential equation and see". The first image is correct, and the other image is correct. Play with frequency of the square wave and see both options.
– user76844
May 6 '16 at 5:56
• en.wikipedia.org/wiki/Gibbs_phenomenon May 6 '16 at 5:58
• The 1st example is 1st order LPF, while the second is n-order LPF. May 6 '16 at 6:17
• Yeah, the cut-off frequency you chose is too low, so you cut harmonics close to the fundamental which is why you don't get the square wave with smooth edges. May 6 '16 at 17:24

There are many types of low pass filter.

The one that gives your 'weird waveform' is the simplest 1st order RC filter. This passes the fundamental signal. However, it not only passes all the higher harmonics, but also gives them a phase shift.

The frequency response of the perfect lowpass filter shown in your image is actually not realisable as a physical filter. One problem is that the infinitely steep transition between passband and stopband requires an infinitely long impulse response, which means that you put a signal into the filter, and the delay is such that you never see it come out!

To this end, we approximate the filter with a shorter, usable impulse response. There are physical component filters, which must be causal (no output occurs before input), and signal processing filters, which can be acausal (in theory, output can occur before input, but any real system that uses them makes them causal by applying a delay).

• Thanks for the answer. So, is it correct to say that the reason that makes the output of the 1st order RC filter be similar to the first wave is the phase shift that appears in higher frequencies and the non-constant attenuation that also appears in higher frequencies? And, if, theoretically, a perfect low-pass filter, like the one in the last image, was applied to a square wave, the output would be the one I was expecting? May 6 '16 at 16:09
• assuming the perfect LPF has no phase shift (which isn't indicated in the magnitude-only transfer response), yes May 6 '16 at 17:32

No real filter has a brick wall response, higher frequencies get through but they get more and more attenuated as you go up in frequency (10dB/Decade I thing for a single low pass filter) chaining more filters in series makes the cutoff slope sharper. But your first waveform looks more lile a capacitor being charged and discharged, try with a smaller C and/or a smaller R (small R allows more current, small C is charged faster, eventually you'll get to a point where it'll look a bit like a sine wave). A square wave is made up of the fundamental frequency (f0) and 3*f0 + 5*f0 + *f0... etc. Each harmonic has a smaller amplitude.

The most obvious difference between the two waveforms is that the first one is assymetric (basically, you're seeing the capacitor charge and discharge exponential curves, which reverse direction at every polarity reversal). The second one has a symmetric pattern.

This is because, regardless of filter "order", they are produced by different kinds of filters.

The first filter is an impulse response filter (in fact IIR—infinite impulse response), which models a behavior seen in common physical systems in the world. In this filter, the output signal is based on past values of the input signal, not on any future values. This causes it to create skewed shapes. If you were to tune that filter so that it allows more higher frequencies through, you would start to see an output which resembles the second one in some ways, but it will not be symmetric. You will see a "squareish" wave, but with decaying "ringing", like in this question. Physical systems like RC circuits and vehicle suspensions are filters of this type. They react to what is happening, and have no information about the future. For example, a vehicle suspension doesn't know that a speed bump or a pot-hole is coming up in the road, and a RC circuit doesn't know that a voltage spike is coming.

The second waveform can be formed by additive superposition of sinusoidal signals: odd harmonics which are in phase. Alternatively, it can be the result of a filter applied to a square wave. But that filter isn't an impulse response filter. Rather, it is a filter whose output, at every point, is based on surrounding points. That is to say, future and past values of the signal. These surrounding values are weighted symmetrically. This is why the smaller "humps" appear nice and symmetric.

Now let us briefly discuss filter order. Low-pass filters do not simply cut frequencies above a certain value; they "roll off" frequencies above a certain "knee". A "first order" filter rolls off at a rate of -6dB per octave. This means that the signal component at twice the knee frequency is diminished by 6 decibels. At three times the knee frequency, it is cut by 12 decibels and so on. As you can see, this keeps going. According to the pure mathematical model, no frequency, no matter how high, is eliminated entirely. A second order low-pass filter rolls off faster, at -12dB per octave.

This is why the the first plot still has discontinuities: the repeated charge-discharge curve has sharp little "cusps" every time the square wave reverses direction. These sharp points represent high frequency components getting through the filter!

The second curve (if taken as the result of filtering a square wave) is a very high-order filter. It is able to let through several low frequency components and then strictly cut everything above. If it weren't a high order filter, it could also produce a waveform with sharp artifacts due to high frequencies not entirely being attenuated.

The skewing or symmetry can also be understood or regarded as an effect of phase. The impulse response filter applies a phase shift to the signal, but the phase shift is frequency dependent. The very low frequencies, far below the "knee", which pass through the filter aren't phase shifted. Frequencies which are heavily attenuated are also phase shifted by up to ninety degrees. And of course, what "ninety degrees" means (in terms of absolute duration or wavelength) is frequency-dependent, too. This frequency-dependent phase shift correlates with the skewed shape. The "ideal" symmetric waveform you see in your second example is free of phase shift among the harmonics. We can produce that waveform additively, by summing together a fundamental sinusoidal together with few low-order odd harmonics which are all perfectly in phase.

I had a similar resulting wave once, and it was the capacitor that was too large - I think the slope in your first image is the capacitor charging and discharging. Maybe you can get closer to your desired behavior by making R larger and C smaller accordingly.