2
$\begingroup$

trying to program an integrator. My input is a square wave and my expected output should be a triangle wave. However, whenever I pass it through my low pass filter algorithm (just a 2nd order butterworth low pass filter with a Q of 0.707), I never seem to get a triangle wave. Instead, I get a a smooth square wave (I guess that's similar to a capacitor smoothing it out?). I am not sure how to tackle this problem as I'm a little new to signal processing + algorithms. Any help would be greatly appreciated.

Below is a picture of an example test I did through Xcode. I was sending a F3 note (around 349 hz) through a low pass with a cutoff of 200 hz and the output is shown below. This is probably the sharpest/closest I've gotten to the triangle wave. enter image description here

EDIT: to clarify, I am trying to convert a square wave to a triangle wave (preferably through a low pass filter if that is totally doable).

$\endgroup$
8
  • $\begingroup$ Why do you expect the low-pass filter to do integration? $\endgroup$
    – MBaz
    Oct 26, 2015 at 21:46
  • $\begingroup$ @MBaz Isn't that how it works in the analog domain? Isn't an integrator just a second order low pass filter? Maybe I am not remembering correctly... $\endgroup$
    – yun
    Oct 26, 2015 at 21:57
  • $\begingroup$ It is true that an integrator performs a sort of low-pass filtering, but they're not the same thing. $\endgroup$
    – MBaz
    Oct 26, 2015 at 22:23
  • $\begingroup$ Hm in that case how should I approach this problem? How can I convert my square wave into a triangle wave? And what is the difference between the two so I know for future knowledge? Thanks! $\endgroup$
    – yun
    Oct 26, 2015 at 22:30
  • $\begingroup$ I don't feel confident enough to offer a solid answer (that's why I've been just commenting :) Let's see if somebody more knowledgeable comes along, otherwise I'll try to put something together. $\endgroup$
    – MBaz
    Oct 26, 2015 at 23:08

2 Answers 2

2
$\begingroup$

The Fourier series of the square wave tells us that the input signal has harmonics at odd multiples of the fundamental frequency $f_1=349\,\text{Hz}$: $$f_k=kf_1,\,\text{$k$ odd}$$ with amplitudes $$A_k=\frac{4A}{k\pi}.$$

The triangular wave, on the other hand, has harmonics at the same frequencies, but their amplitudes are $$B_k=\frac{4A}{k^2\pi^2}.$$

In consequence, your filter needs to have gain $\frac{B_k}{A_k}$ at frequency $f_k$. Any other set of gains will result in an output different from a triangular wave. Note that I haven't mentioned the phase, but you need to make sure your filter has linear phase. Otherwise the triangular wave will be distorted.

Note that an ideal integrator will do the trick. A practical integrator may do it too, but its bandwidth needs to be large enough to not introduce additional attenuation to the higher frequency harmonics.

$\endgroup$
1
  • $\begingroup$ After doing a few tests, adjusting the filter gain is what led me to my expected triangle output. Thanks for all your help! The other answer was good but this is what ultimately gave me my answer. $\endgroup$
    – yun
    Oct 27, 2015 at 15:45
4
$\begingroup$

If your square wave has a mean of zero (this is important!), then a simple accumulator can do the job. Its operation is described by

$$y[n]=x[n]+y[n-1]$$

where $x[n]$ is the input (square wave) and $y[n]$ is the output (triangular wave).

This is a simple Matlab/Octave script showing how it works:

sq = [1,-1,1,-1,1,-1,1,-1]'*ones(1,5);
sq = sq'(:);                 % a few periods of a square wave
tr = filter(1,[1,-1],sq);    % filtered by accumulator
% plot
subplot(2,1,1),stem(sq)
subplot(2,1,2),stem(tr)

enter image description here

$\endgroup$
1
  • $\begingroup$ This is a good answer but ultimately MBaz's answer was what I needed. My filter gain for my biquad filter had to be adjusted. Thanks though, this is informative! $\endgroup$
    – yun
    Oct 27, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.