# Square Wave with Random Edge Shifting

I took a square wave signal and individually shifted all the rising and falling edges by a random amount of time, within certain +/- bounds (a random time for each edge, not shifting all edges by the same time). This is what the FFT looked like before the edge shifting:

After the random edge shifting, the FFT looks like this:

I did not expect to still see sharp peaks at the signal frequency and odd harmonics. Rather, I expected that the peaks would become wider, with a width corresponding to the maximum frequency deviation that the time shift bounds allowed. Can someone explain what is going on here?

Edit: How is it different than this?

• your signal is still periodic, but with jitter. So, this isn't surprising! It means you've used a random variation with a limited variance to shift the edges. (Check whether my statement is correct!) – Marcus Müller Aug 21 '18 at 16:21
• I thought that jitter shows up as sidebands in the frequency domain. Is this not correct? Also, I edited my original post to show an example of what I thought would happen. In this example a square wave is modulated with a triangle wave. I thought that randomly shifting the edges would give a similar result, where the average frequency is where the peak used to be but the signal now contains multiple frequencies around the center since it is being modulated, in my case with random modulation. – dsphelp123 Aug 22 '18 at 12:12

By separately changing both the leading edge and the trailing edge of each pulse, you have converted the square wave into what can be viewed as an asymmetric double-edge pulse-width-modulated (DEPWM) signal. Analysis of the Fourier spectra of various types of deterministic PWM signals (including the type mentioned in the previous sentence) can be found in the paper Z. Song and D. V. Sarwate, "The Frequency Spectrum of Pulse Width Modulated Signals", Signal Processing, vol. 83, pp. 2227-2258, 2003. (The article is available behind the publisher's paywall but there are some free copies floating around the web). The results show that the spectra of PWM signals do have the spikes shown in the OP's diagram just as the square wave spectra do, but there is additional clutter between the peaks. That is, the PWM Fourier spectrum is not a Fourier series like the square wave spectrum is; PWM spectra are of a continuous-time signal that contains periodic components (giving those spikes) as well as non-periodic components giving the clutter. The details (and the nuances) are too messy to transcribe here; interested readers should read the original.

This isn't surprising.

So, let's look at one of these peaks: What does having a peak there mean?

Right, the FFT is just an implementation of the DFT. A peak at frequency $k$ means that the sum

$$S[k] = \sum_{n=0}^N s[n]e^{-j2\pi \frac kN n}$$

is large.

It's pretty easy to imagine how that happens:

See thee $e^{jx}$ term in that sum. That's a complex sinusoid of relative frequency $\frac kN$. Now, what that sum does is simply taking a vector containing (equidistant samples of) that sinusoid, and forming the dot-product with the time-sample vector $s$.

Geometrically speaking, we're projecting your signal onto a sine of a given frequency, and get a complex value, whose amplitude tells us "similarity" (phase of that complex value is phase of the signal, but we don't care about that for now).

Let's reduce this to the real-valued case; it's easy to go back to complex later:

Now, what happens when you draw you rectangular wave, and on top of that, a cosine that has its maximum right at the center of each positive halfwave, and then do a point-wise product at equidistant sampling points?

Right, you get a very high value, because you're effectively calculating something like the integral of the abs() of the cosine.

Now, jitter the edges a bit. Does that change things much?

No. The edges fall in places where amplitude of the cosine isn't high to begin with. Sure, you lose some energy in your overall sum, but it's not going to make that high product value go away.

That's exactly what you observe here. Shifting the edges around a bit does reduce the energy in the harmonics of the square wave's frequency a bit, but not much. (You can see that you're actually distributing energy from the harmonics onto your wideband noise floor, for example, at the harmonic third harmonic from the left.)

I don't know why you're doing this, but for example in switch-mode power supplies, you need to switch a transistor on and of pretty often. Now, doing so with a square wave will introduce strong spectral peaks (as you've demonstrated!), which will lead to (potentially harmful and illegal) RF emissions.

So, some switch-mode power supplies use what is called a spread-spectrum clock, which doesn't only jitter a few edges – it inverts (seemingly) random half waves! Only on average that clock is half-time on, half-time off, but in between, it's a binary pseudorandom sequence.