Integration of square wave 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. 


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).
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)