55
$\begingroup$

I would like to know what's the equation of a "tilted" sine, that looks like this (no idea how to show it better).

enter image description here

I remember first seeing this waveform in some kind of sound synthesizer, where one of the knobs for controlling shape of the sine was doing just what im looking for - gradually turning sine to sawtooth and vice versa.

I tried using fourier series on a sawtooth wave, and getting a couple of first sines together, but the result doesn't have that smoothness.

$\endgroup$
4
  • 7
    $\begingroup$ try $\sin\left(x+\frac{\sin(x)}{2}\right)$ $\endgroup$
    – Wouter
    Sep 15, 2017 at 14:17
  • 1
    $\begingroup$ The wonderful oscilloscope and function generator combination! O' how I miss them so. $\endgroup$ Sep 15, 2017 at 15:00
  • $\begingroup$ Related math.stackexchange.com/questions/1468794/… $\endgroup$ Sep 16, 2017 at 21:14
  • 1
    $\begingroup$ When you use a truncated fourier series to synthesize a waveform, cut the last term in half and you will get a much smoother approximation. This is similar to apodizing in optics and antenna design, whereby we try to suppress diffraction rings and sidelobes. $\endgroup$ Sep 23, 2017 at 20:48

8 Answers 8

51
$\begingroup$

One can look at the derivative of the function and see that near $0$, the derivative is large and near half the period, it is almost constant. This hinted that the derivative might be a cosine raised to an even power minus a constant. The constant would need to be chosen to cancel the integral of the even power of cosine over a period.

For example, $\cos^8\left(\frac x2\right)-\frac{35}{128}$:

enter image description here

The integral is $\bbox[5px,border:2px solid #C0A000]{\frac7{16}\sin(x)+\frac7{64}\sin(2x)+\frac1{48}\sin(3x)+\frac1{512}\sin(4x)}$:

enter image description here


We can generalize this by noting that $$ \cos^{2n}\left(\frac{x}2\right)-\frac1{2^{2n}}\binom{2n}{n}=\sum_{k=1}^n\frac{\binom{2n}{n-k}}{2^{2n-1}}\cos(kx) $$ Then we get that the integral is $$ \bbox[5px,border:2px solid #C0A000]{\sum_{k=1}^n\frac{\binom{2n}{n-k}}{k\,2^{2n-1}}\sin(kx)} $$ The case illustrated above is $n=4$.


Scaling by $\frac{2^{2n-1}}{\binom{2n}{n}}$ gives that the integral of $$ \frac{2^{2n-1}}{\binom{2n}{n}}\cos^{2n}\left(\frac{x}2\right)-\frac12 $$ enter image description here

is $$ \bbox[5px,border:2px solid #C0A000]{\sum_{k=1}^n\frac{\binom{2n}{n-k}}{\binom{2n}{n}}\frac{\sin(kx)}k} $$ enter image description here

which, as $n\to\infty$, tends to $$ \sum_{k=1}^\infty\frac{\sin(kx)}k $$ which is a sawtooth wave.

$\endgroup$
6
  • $\begingroup$ interesting intuition that of $cos^{2n}$ $\endgroup$
    – G Cab
    Sep 16, 2017 at 23:28
  • $\begingroup$ A beautiful series of approximates, and a very nice approach to get there. $\endgroup$ Sep 18, 2017 at 4:03
  • $\begingroup$ Which grapher did you use for plotting multiple graphs at the same time? $\endgroup$ Nov 29, 2017 at 5:45
  • 1
    $\begingroup$ @JaideepKhare: The images were done with Mathematica. $\endgroup$
    – robjohn
    Nov 29, 2017 at 15:41
  • $\begingroup$ @robjohn OK. Thanks for information. $\endgroup$ Nov 29, 2017 at 15:42
48
$\begingroup$

You can try this :

$$y=\sin \left( x+\dfrac yn \right)$$

Here $n \in \mathbb R -\{0\}$. Positive $n$ will "tilt" the graph left side, while negative $n$, right side.

For example,

When $n=1$, $y=\sin(x+y)$

enter image description here

When $n=2$, $y=\sin \left( x+\dfrac {y}{2} \right)$

enter image description here

When $n=10$, $y=\sin \left( x+\dfrac {y}{10} \right)$

enter image description here

As $n \to \infty$, $y=\sin(x)$

enter image description here

$\endgroup$
10
  • 21
    $\begingroup$ +1 Interesting function defined implicitly. I suspect the OP would prefer something explicit, even as a series if not closed form. $\endgroup$ Sep 15, 2017 at 14:01
  • $\begingroup$ If you replace the "y" inside the sine with "sin x" (which is of course the same thing when the skew factor is 0) you get a somewhat similar plot providing that factor isn't too large. Up to about sin(x + 1/3 sin x) it looks pretty good to me. $\endgroup$ Sep 15, 2017 at 14:42
  • 1
    $\begingroup$ If you want to skew it more you can kinda find the fixed point by hand: go from sin(x) to sin(x+k sin x) to sin(x + k sin(x + k sin x)) and so on. In the limit you get exactly the function defined implicitly here, unless I'm confused. $\endgroup$ Sep 15, 2017 at 14:43
  • $\begingroup$ To my eye: 0 iterations are good at k=0, 1 iteration is good for k<=1/3, 2 iterations are good for k<=1/2, 3 iterations are good for k<=2/3. Approximately. $\endgroup$ Sep 15, 2017 at 14:48
  • 1
    $\begingroup$ @GaurangTandon No logic, it's just free internet (Thanks to JIO), free time, Desmos grapher, and just a crazy idea, which came out of nowhere. $\endgroup$ Jan 20, 2018 at 6:34
32
$\begingroup$

In the old signal generators, the section devoted to produce square and sawtooth waves, was commonly realized starting from a pulse train generator, in which the frequency and the pulse duration (duty cycle) were tunable.
From that, by (analogic) integration, and clipping a series of triangular and trapezoidal waves could be obtained.

The knob of your synthetizer was probably acting on a filter applied to a sawtooth wave. A low-pass (e.g. RC, or higher degree) filter should do the job.

The output of a simple RC filter

Tilted_sine_1

is given by the differential equation $$ v_{{\rm in}} (t) - v_{{\rm out}} (t) = RC{d \over {dt}}v_{{\rm out}} (t) $$

When the input is a discontinuous function, and since we are interested in the long term component of the output (the particular solution to the ODE above), it is not practical to apply the differential equation above directly, and we have better go by expressing the $v(t)$'s through a formal series, in practice the Fourier series.

A sawtooth wave, with duty cycle $\tau$

Tilted_sine_2

$$ v_{{\rm in}} (t) = \left\{ {\matrix{ {2/\tau \,t} & {0 \le t < \tau /2} \cr {{1 \over {1 - \tau }}\left( {1 - 2t} \right)} & {\tau /2 \le t < 1 - \tau } \cr {2/\tau \,\left( {t - 1} \right)} & {1 - \tau /2 \le t < 1} \cr } } \right. $$ which is the integral of a pulse train with same $\tau$, has the Fourier series expansion $$ v_{{\rm in}} (t) = {{2\tau } \over {1 - \tau }}\sum\limits_{1\, \le \,n} {{{\sin \left( {\pi n\,\tau } \right)} \over {\left( {\pi n\,\tau } \right)^{\,2} }}\sin \left( {2\pi nt} \right)} $$

Without resorting to complex representation and impedance, it is quite simple to introduce a generic component of the input into the differential equation and deduce that we will obtain $$ v_{{\rm out}} (t) = {{2\tau } \over {1 - \tau }}\sum\limits_{1\, \le \,n} {{{\sin \left( {\pi n\,\tau } \right)} \over {\left( {\pi n\,\tau } \right)^{\,2} \sqrt {1 + \left( {2\pi nRC} \right)^{\,2} } }}\,\sin \left( {2\pi nt - \arctan \left( {2\pi nRC} \right)} \right)} $$

Limiting the sum to the first few components (10 in this example) and plotting we get

enter image description here

Finally, a comment on why the OP did not get a smooth result by taking the first few components of a sawtooth.
Doing that corresponds to apply an ideal low-pass filter, which is not a physically realizable device.
The RC filter is instead a physical device which, in spite of not being perfect, but introducing some attenuation and shift of phase in the frequency domain, provides the "smoothness" of the output signal.
The plot below shows the sum of the first $3$ harmonics from a sawtooth, and the corresponding first $3$ harmonics from the RC filter output.

Tilted_sine_4

$\endgroup$
7
  • 7
    $\begingroup$ This answer is good because it explains what's really going on. It would be even better if you explained to the poster (and for anyone else who stumbles upon this question) what a low-pass filter is. $\endgroup$ Sep 16, 2017 at 2:35
  • $\begingroup$ @MackTuesday: I did not expect so much interest in such an old analogic (..!) "relic". Thanks for signalling that. I expanded (compatibly with space here) my answer: wish it is more explicit now. $\endgroup$
    – G Cab
    Sep 16, 2017 at 17:33
  • $\begingroup$ I'd remark that a first-order filter like RC can't actually get very close to a sinusoidal signal. Higher-order filters can. See my answer for a couple of signal examples with different filter coefficients. $\endgroup$ Sep 16, 2017 at 17:55
  • $\begingroup$ @leftaroundabout: but a "tilted" sine is not a pure sinusoidal signal, it contains harmonics ! $\endgroup$
    – G Cab
    Sep 18, 2017 at 22:37
  • $\begingroup$ @GCab sure, just, the question said “gradually turning sine to sawtooth and vice versa”. $\endgroup$ Sep 19, 2017 at 8:32
24
$\begingroup$

I suggest taking

$$\frac1t\tan^{-1}\frac{t\sin x}{1-t\cos x}$$

with $-1\leq t\leq+1$ where the two extreme values of $t$ give sawtooths in opposite directions, and $t=0$ is simply a sinusoid. Intermediate values of t give nice smooth functions that do indeed look like "sheared sinusoids".

(When $t=0$ exactly, taking the formula literally gives you 0/0. The limit as $t\rightarrow0$ is $\sin x$, and for values of $t$ very close to 0, it may be numerically better to take a few terms of the series you'll find below under the heading "Motivation".)

If you would like to play with this, you can do so e.g. by going to https://www.desmos.com/calculator, pasting in this formula \frac{1}{t}\arctan \left(\frac{t\sin \left(x\right)}{1-t\ \cos \left(x\right)}\right), and setting the permitted range in the slider for $t$ to -1..+1.

Here are plots for t=0.25, t=0.5, t=0.75, and t=0.95.

enter image description here enter image description here enter image description here enter image description here

Motivation:

A sawtooth wave is given by $\sum\frac1n\sin nx$. So we might want something that interpolates between (1,0,0,...) at 0 and (1,1/2,1/3,...) at 1. Here's an obvious way to do it: take $\sum \frac{t^{n-1}}n\sin nx$ where $t=0$ for an ordinary sinusoid and $t=1$ for a sawtooth.

It is not surprising that this has a nice closed form (think of the sine as the difference of two complex exponentials; then our series is the difference of two complex logarithms); it turns out to be the formula above. (Thanks to Bob Hanlon in comments for supplying this; the difference between what he wrote and what I have above is because of an off-by-one error in an earlier version of this answer.)

As well as being the motivation for the answer above, this series may be of some practical use. At $t=0$ the closed-form formula is singular; when $t$ is nonzero but very small it's possible that you may do better numerically to use a few terms of the series rather than the closed-form formula.

Relationship to some other answers:

Our function is obtained from a sawtooth by multiplying the terms of its Fourier series by $(\dots,t^2,t^1,t^0,?,t^0,t^1,t^2,\dots)$. (The series is two-sided; the $\sin nx$ term is a combination of $e^{inx}$ and $e^{-inx}$, which both need to be multiplied by $t^{n-1}$; we can replace the "?" with anything we like because the constant term in the Fourier series we're working with is zero.) Pointwise multiplication of Fourier series equals convolution of functions. One choice of that "?" makes the function we're convolving with equal $2\frac{\cos x-t}{1+t^2-2t\cos x}$; note that when $t=0$ this is just a sinusoid and that as $t->\pm1$ it approaches a delta function. So we can, indeed, think of this as starting with a sawtooth function and applying a linear filter that varies smoothly between doing nothing (convolving with a delta function) and smoothing everything into perfect sinusoids (convolving with a sinusoid).

$\endgroup$
6
  • $\begingroup$ It clearly does (the series is basically the one for log(1-z) where z gets a real part from the exponential decay and an imaginary part from the sines. I have to be AFK for a bit so won't expand on this in my answer immediately. $\endgroup$ Sep 15, 2017 at 17:35
  • 4
    $\begingroup$ The closed form of the series assuming 0 < t < 1 is ArcTan[(t Sin[x])/(1 - t Cos[x])] $\endgroup$
    – Bob Hanlon
    Sep 15, 2017 at 17:47
  • $\begingroup$ Ah, this is $1/t$ times the angle between the $x$-axis and the line $OP$, where the point $P$ moves along a circle of radius $t$ centered at $(1,0)$. $\endgroup$
    – user856
    Sep 15, 2017 at 22:30
  • $\begingroup$ How could 0 could give a sinusoid in this formula? 1/0? $\endgroup$
    – Lugi
    Sep 18, 2017 at 16:10
  • $\begingroup$ As $t\rightarrow0$ the function $\rightarrow\sin x$. I'll edit the answer to clarify that a bit. $\endgroup$ Sep 18, 2017 at 16:49
14
$\begingroup$

Let's free-hand Fourier transform! First, we sketch what we want on top of an actual sine function. Draw one period. We notice that in that period, the sine function is first too low, then too high, then too low, then too high again. That means that the difference between what we have and what we want has two tops and two bottoms, kindof like $\sin(2x)$. So we add that.

This time you have to plot the graph to see what happens. Let's try with $\sin(x) + 0.3\sin(2x)$. OK, we're getting the tilt we want, but on the way down the graph has this weird wavey thing. Back to the drawing board.

Now sketch the graph you had together with the graph you want. At least when I did this, the graphs crossed one another a number of times. $6$ to be exact, alternating which one is largest and which is smallest. Thus the difference has three maxes and three mins in this period, kindof like $\sin(3x)$. So we try to add that.

Now, with $\sin(x) + 0.3\sin(2x) + 0.065\sin(3x)$ it's starting to look good. You can probably tweak the coefficients here a bit to get something even better. Just keep going until you're happy. You can also add higher frequency terms if you want, which will give you more tilt. But it will get more difficult to gauge exactly how to compensate out the high frequency waving.

Alternatively, you could try to sketch the derivative you want, and do the same with that, only with cosines. Higher frequency waves will be more prominent then, and hopefully easier to correct for.

$\endgroup$
2
  • 1
    $\begingroup$ Love this explanation. All too often I see students simply cranking the handle without really understanding what they are doing. And this is great for the more visually inclined learner's. $\endgroup$ Sep 16, 2017 at 17:16
  • 1
    $\begingroup$ Should be 0.065 not 0.65. $\endgroup$
    – Moss
    Apr 18 at 1:33
8
$\begingroup$

You can look into the Clausen function which is a class of functions with this form and which has been implicitly described by another answer.

$\endgroup$
7
$\begingroup$

I think G Cab's answer is the best. Wouter's comment is also good because it invokes phase distortion and modulation, which are common techniques used in synthesizers.

As for my contribution, a professional developer of software synthesizers (who isn't me) shared an interesting idea on the forums over at KVR Audio.

Go to https://www.desmos.com/calculator. Copy and paste the following equation.*

((a+\cos x)\cos n+\left(b\sin x\right)\sin n)/\sqrt{(a+\cos x)^2+\left(b\sin x\right)^2}

Click on the "all" button next to "add slider".

For the a and b sliders, change the range from [-10,10] to [0,1].

Now you can fiddle with the kinds of waveforms this scheme generates.

For your case, try using $(a,b,n) = (0.9,1.0,-1.6)$. The phase is off by 90 degrees but that's sonically irrelevant by itself.


* This technique is inspired by a geometric construct. The equation in human-readable form is

$$\frac{(a+\cos x)\cos n+b\sin x\sin n}{\sqrt{(a+\cos x)^2+\left(b\sin x\right)^2}}$$

$\endgroup$
1
  • 1
    $\begingroup$ Thanks for the appreciation, and thanks for providing this interesting waveform. $\endgroup$
    – G Cab
    Sep 16, 2017 at 17:55
6
$\begingroup$

http://nbviewer.jupyter.org/gist/leftaroundabout/725e015c9ea7a2f61cd8f2eec4028dff

As G Cab said, a synthesizer generates such a waveform by low-pass filtering a sawtooth signal.

To describe that mathematically: we start out with a sawtooth $$ s(t) = \operatorname{round} (t) - t. $$ Plain sawtooth wave

The low-pass filter is typically a kind of state variable filter. These are named so because there is a “state variable” that's governed by an ordinary differential equation dependent on the incoming signal. The simplest form, that can be build with just a resistor and a capacitor, can be written (I'm using PDE-convention notation for derivatives, i.e. $r_t = \frac{\partial r}{\partial t}$) $$ r_t(r,t) = \eta \cdot (s(t) - r(t)) $$ Because $r_t$ depends Lipschitz-continuously on $r$, the Picard-Lindelöf theorem tells us that the solution $r : \mathbb{R}\to\mathbb{R}$ is uniquely determined by this differential equation, in other words, the above equation for $r_t$ also defines $r(t)$.

But how does it actually look? Well, we can use numerical techniques to calculate an arbitrarily good approximation to the exact solution. A common method is the fourth-order Runge-Kutta solver. Implemented in Haskell:

rk₄ :: (Time -> ℝ -> ℝ) -- the function r_t(t,r)
    -> Time             -- time-step for the solver
    -> [(Time, ℝ)]      -- sequence of solution snapshots [r(t)]
rk₄ f h = go 0 0
 where go t y = (t,y) : go (t+h) (y + h/6*(k₁ + 2*k₂ + 2*k₃ + k₄))
        where k₁ = f t y
              k₂ = f (t + h/2) (y + h/2*k₁)
              k₃ = f (t + h/2) (y + h/2*k₂)
              k₄ = f (t + h) (y + h*k₃)

If we evaluate this for the above filter $r$ with a couple of different values for $\eta$, we get this result:

toRenderable $ forM [0 .. 8] $ \η -> signalPlot ("η = "++show η)
          $ rk₄ (\t r -> η * (s t - r)) 0.01

Sawtooth after 1st order linear filter

Well, this does appear to behave broadly related to the function you've described, but it's hardly the same thing. In particular, the result you get for low $\eta$ doesn't actually look much like a sine, it's still pretty spiky on the negative side, but already has a strongly attenuated amplitude.

And indeed that's also the case for a real analogue synthesizer: those never generate exact sine signals, only approximations. Im my example the deviation is extreme because I've used a very simple filter, which has only order 1. I.e., when I make $\eta$ small enough to get rid of the transient high-frequency components (the “edges”), then I must also sacrifice a lot of the base sinusoidial part.

With a suitable second-order filter, we can get a better result. That requires an additional variable $u$ to be solved:

toRenderable $ forM [0, 5 .. 30] $ \η -> signalPlot ("η = "++show η) $ second fst
          <$> rk₄ (\t (r,u) -> (u, η^2 * (s t - r) - η*u)) 0.01

Sawtooth after 2nd order linear filter

Still not perfect, but a really well-designed filter can get you very close to a real sine. Filter design used to be a big research topic. Nowadays it tends to be more practical to use digitally sampled signals, then you can just perform a Fourier transform, take whichever frequency components you want, and zero out or attenuate the rest to any amplitude you like.


RK4 is arguably a bit overkill here. Because this filter is linear, a discretised form can actually be calculated much more efficiently and accurately as an IIR, using linear filter theory.

If the goal is really only to generate a sine, then you can actually get much more out of a simple 2nd-order filter than in my example, by using high resonance peak: you start out with only a very weak rectangular signal that's feeding a resonant LC circuit at the right frequency so the the output has a much higher amplitude. But this requires that the filter and oscillator are exactly in tune – in practice, they're generally back-coupled to a phase-locked loop, i.e. the oscillator is actually part of the filter resonance.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .