# Generating smoothed versions of square wave, triangular, etc

I'm trying to get functions depicting smoothed out versions of wave forms(triangle,square, sawtooth, reverse sawtooth) which have the same amplitude and frequency as $cos(x)$.

I found a few trigonometric functions depicting such wave forms:

$\delta=0.05$,

$Square_{smooth}=2.1\tan^{-1}(\sin(\pi x/3)/\delta)/\pi$

$Triangle_{smooth}=1.25-5\cos^{-1}((1-\delta)\sin(\pi x/3.2))/(2\pi)$

$h=12.5$,

$Sawtooth_{smooth}=-1.5 + 3((x/6-0.5) -(\tanh((((x/6-0.5)+0.5)-\lfloor{(x/6-0.5)+0.5}\rfloor-0.5)h)/(2\tanh(0.5h))+\lfloor{(x/6-0.5)+0.5)}\rfloor-0.5))$

but they do not have the same amplitude or frequency unfortunately. I think $-cos(x)$ might work for $Triangle_{smooth}$, but I'm still looking for ways to fix $Square_{smooth}$ and $Sawtooth_{smooth}$, anyone have any ideas?

• What do you mean by "smooth"? Have you tried adding the first few harmonics of each type of waveform? – MBaz Oct 31 '16 at 13:05
• I wanted to do something similar to what was described over here:mathematica.stackexchange.com/questions/38293/…. I did try to see how plotting the first few terms of the fourier series of the square wave forms would look like but it wasn't ideal. – gmmajal Oct 31 '16 at 13:30
• The formulas in your link seem to be designed to produce a signal of frequency 1 and amplitude from -1 to 1. So, it seems that all you need to do is scale the argument of $\sin$ by $2\pi$? Also, is there a reason for not using the exact same values as in the linked equations? – MBaz Oct 31 '16 at 14:39
• @gmmajal double-check the parentheses in your last equation. – Gilles Oct 31 '16 at 19:55

So, a "smooth" (differentiable, without Gibbs effects) square wave can be defined as $$s_{ss}(t)=\frac{2A}{\pi}\arctan\left[\frac{\sin(2\pi tf)}{\delta}\right].$$ (Source.) The parameter $$A$$ controls the amplitude, and $$f$$ the frequency. For example, using Matlab with $$A=1.5$$ and $$f=2$$:

t = 0:1/100:2;
delta = 0.01;
A = 1;
f = 2;
smoothsq = (2*A/pi)*atan(sin(2*pi*t*f)/delta);
plot(t,smoothsq);
axis([-0.2 2.2 -1.6 1.6]);


produces the plot:

Something similar should work for the other waveforms.

• Thanks for the clarification, the problem I'm having is that I'm not able to get the frequencies of the wave forms to match. See the image here: imgur.com/a/L6GCX, I used f=1 and A=1. The best result that I got was with f=1/6, but I couldn't any better than that. If you look at the original source there the frequencies are synchronized perfectly and that's what I would like to have. Were you able to get the exact same result as given in the source when plotting the smooth versions with the built in matlab functions as well? – gmmajal Oct 31 '16 at 16:06
• @gmmajal It works fine here, see imgur.com/a/GH3X9 where I used f=2. – MBaz Oct 31 '16 at 16:38
• \@MBaz I don't fully understand how the x-axis in your case is scaled.I don't think cos(x) should complete its entire cycle at 0.5 unless you're changing the frequency of cos. I copied your script and I tried the case with f=2, my results are totally different imgur.com/a/5y01Y. – gmmajal Oct 31 '16 at 17:29
• Just a hunch here: a cosine of frequency $f$ is written as $\cos(2\pi ft)$. Are you defining the cosine like that? – MBaz Oct 31 '16 at 17:31
• No I was't and there lies the mistake. In my case $2\pi f=1$, so I did the same for the smoothed version and now the results match. Thanks for clarifying this, I'll try doing the same with the other functions as well. – gmmajal Oct 31 '16 at 17:39

It seems the amplitude is not scaled properly. Rather than (2*A/pi) using (A/atan(1/delta)) seems more appropriate. In other words I propose:

y = (A/atan(1/delta))*atan(sin(2*pi*t*f)/delta);

Below is a figure illustrating the difference between the two scaling approaches. For low delta values the difference is not clear but for high delta values the amplitude departs from what is desired. The images can be reproduced using the MATLAB code below.

t=linspace(0,2*pi,500);
delta = 0.1;
A = 1;
f = 1/(2*pi);
y1 = A*sin(2*pi*t*f);
y2 = (2*A/pi)*atan(sin(2*pi*t*f)/delta);
y3 = (A/atan(1/delta))*atan(sin(2*pi*t*f)/delta);

figure; hold on;
h1=plot(t,y1,'r-','LineWidth',5);
h2=plot(t,y2,'g-','LineWidth',4);
h3=plot(t,y3,'b-','LineWidth',2);
legend([h1 h2 h3],{'Sine','Smooth square wave','Corrected smooth square wave'},'Location','SouthOutSide');
axis tight; axis square;
set(gca,'FontSize',15); grid on; box on;
drawnow;