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:


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


$ 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?

  • 1
    $\begingroup$ What do you mean by "smooth"? Have you tried adding the first few harmonics of each type of waveform? $\endgroup$
    – MBaz
    Oct 31, 2016 at 13:05
  • $\begingroup$ 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. $\endgroup$
    – gmmajal
    Oct 31, 2016 at 13:30
  • $\begingroup$ 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? $\endgroup$
    – MBaz
    Oct 31, 2016 at 14:39
  • $\begingroup$ @gmmajal double-check the parentheses in your last equation. $\endgroup$
    – Gilles
    Oct 31, 2016 at 19:55

2 Answers 2


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);
axis([-0.2 2.2 -1.6 1.6]);

produces the plot:

enter image description here

Something similar should work for the other waveforms.

  • $\begingroup$ 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? $\endgroup$
    – gmmajal
    Oct 31, 2016 at 16:06
  • $\begingroup$ @gmmajal It works fine here, see imgur.com/a/GH3X9 where I used f=2. $\endgroup$
    – MBaz
    Oct 31, 2016 at 16:38
  • $\begingroup$ \@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. $\endgroup$
    – gmmajal
    Oct 31, 2016 at 17:29
  • $\begingroup$ Just a hunch here: a cosine of frequency $f$ is written as $\cos(2\pi ft)$. Are you defining the cosine like that? $\endgroup$
    – MBaz
    Oct 31, 2016 at 17:31
  • $\begingroup$ 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. $\endgroup$
    – gmmajal
    Oct 31, 2016 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.

Animation for original and adjusted functions

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;
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;

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.