# Why are there beats in spectrogram / Windowed Fourier Transform of sines?

I am trying to understand how the Windowed Fourier transform works, so I used Matlab to create an artificial signal, and plotted the Windowed Fourier transform using a gaussian window.

$x(t) = \left\{\begin{matrix} \sin(2\pi100t)& \text{for }0\leq{t}<\frac{1}{4}\\ \sin(2\pi50t)& \text{for }\frac{1}{4}\leq{t}<\frac{1}{2}\\ \sin(2\pi25t)& \text{for }\frac{1}{2}\leq{t}<\frac{3}{4}\\ \sin(2\pi10t)& \text{for }\frac{3}{4}\leq{t}\leq1 \end{matrix}\right.$

and $f_s=500$, i.e. the data points are sampled at every 1/500 second.

This is what I have obtained for my plot.

However, for the last 2 intervals, $\frac{1}{2}\leq{t}<\frac{3}{4}$, and $\frac{3}{4}\leq{t}\leq1$, we can see some form of bifurcating phenomenon (vertically). Why is this so?

• I think you meant to say that $f_s$ = 500, not $1/500$. Commented Oct 28, 2013 at 5:16
• If you think that there's not enough detail in the answers you got you should also try to ask more specifically for what you do not understand yet. I wouldn't know what aspect of the question to elaborate on unless you give detailed feedback. Commented Nov 10, 2013 at 11:12
• @freak_warrior, I though that is what I did below. If you don't tell me which aspect remains unclear I don't see what I could add to that explanation. Commented Nov 10, 2013 at 15:12
• It would be great if you could reply in the same comment thread and not jump threads. I posted my answer below where it belongs. Commented Nov 13, 2013 at 10:20
• @OverLordGoldDragon Reopened! Work your magic.
– Peter K.
Commented Mar 15, 2023 at 16:14

This happens because your window is too short. I don't have access to a plotting tool right now, but imagine for a second a slowly varying sinusoid that you chop up into pieces, and these pieces are shorter than the period of your sinusoid. If you take the Fourier transform of each of these pieces, some of your chunks will capture more energy of this sinusoid than others because they will contain crest or through of the function, while others will contain regions close to 0.

If you increase your window size, you'll be able to catch several cycles of your slow sinusoids inside each window, and the periodicity will go away. The tradeoff is that your windows will capture larger lengths of time, so boundaries between events will be more blurred.

If this still doesn't make sense, I'll try to include some plots later.

• This picture is incomplete; refer to my answer. I otherwise really like this manner of explanation. Commented Mar 16, 2023 at 16:08

In discrete signal processing the frequency domain axis topology is surprisingly not a straight line, but a closed circle. Therefore the upper and lower edge of your image are really "identified", meaning they are connected and direct neighbors. The modulation you're seeing is the beating of the two interfering components coming closer.

The window size has an effect on this as it controls the frequency resolution. If you take an (effectively) longer window, then the beating will be reduced because the fourier transforms of the window centered at the sinusoidal components' frequencies will overlap less.

• @freak_warrior, let me know what remains unclear after reading en.wikipedia.org/wiki/Beat_(acoustics) Commented Nov 2, 2013 at 18:23
• @freak_warrior, not sure I understand what you're asking. The beating is the modulated envelope that you see for the last two sinusoidal bursts. Commented Nov 6, 2013 at 9:28
• You can reduce the beating by taking a longer analysis window, like I wrote in my answer. But you can never eliminate the phenomenon of beating itself. If two sinusoids are close enough the frequency resolution of the spectrogram will not suffice to resolve them and they will start to beat. Commented Nov 10, 2013 at 18:38
• The strength (and frequency) of the beating depends on the distance of the sinusoids in the frequency direction. If you increase the distance, the beating gets weaker and also faster, until it is both too fast and too weak to be noticed. And like I said in my answer, the top and bottom boundary of your spectrogram are really a shared edge, like in a cylinder. The two bars from the sinusoid's negative and positive frequency component are therefore very close together, and not far apart as one would think. Commented Nov 12, 2013 at 9:00
• @freak_warrior, I said "bars from the sinusoids negative and positive frequency component", so it should be clear that I mean the horizontal bars. The vertical transient-like features are the result of the beating (on-off-on-off) modulation. Commented Nov 13, 2013 at 10:22

To illustrate what both @Jazzmaniac and @Phonon are telling you, let's look at the same plot, but for different window lengths. Another change is to look at the plots using the fftshift view --- so that it's clearer that the low positive frequency peaks are close to the low negative frequency peaks.

The picture below plots the contour plot of the data you have (see scilab code below) for window lengths of 32, 64, 128 and 256. Even at the 256 window length, the effect of the "beating" is still somewhat present.

Code only below.

fs = 500;
t1 = [0:1/fs:(0.25-1/fs)]
t2 = [0.25:1/fs:(0.5-1/fs)]
t3 = [0.5:1/fs:(0.75-1/fs)]
t4 = [0.75:1/fs:(1-1/fs)]
T = [t1 t2 t3 t4];

x = [sin(2*%pi*100*t1) sin(2*%pi*50*t2) sin(2*%pi*25*t3) sin(2*%pi*10*t4)];

N = length(x);
figNo = 1;

figure(1)
clf
subplot(221)
xset("fpf","   ")
for window_length = 2.^[5:8];

Z = [];
X = [];
Y = (([1:window_length]-1)/window_length*fs - fs/2);
for k=1:10:N
last_index = min(N,k+window_length-1);
disp(last_index)
X = [X; k];
end

subplot(2,2,figNo);
figNo = figNo + 1;
contour(T(X),Y,Z,4,flag=[2,0,4])
title("Window Length = " + string(window_length) )
xlabel('Time');
ylabel("Frequency (Hz)")
//plot3d(X,Y,Z/max(Z)*10,alpha=60,theta=50,flag=[2,3,0],ebox=[0 N -fs/2 fs/2 0 10])
end

• Oh wow, sweet plots! = ) Commented Nov 15, 2013 at 18:15

The culprit is analyticity: having non-zero negative frequencies.

"Windowed Fourier Transform" is one perspective of STFT; fundamentally, it's convolutions of windowed complex sinusoids with the input, i.e. bandpass filtering. Explained here.

From convolutions perspective, the picture is obvious; here's the filterbank:

Conv in time $$\Leftrightarrow$$ mult in freq. Hence, each STFT row is generated by multiplying a STFT filter by input's spectrum, then taking ifft. A pure sine is two unit impulses in frequency - thus, we're taking ifft of their re-weightings:

Shown in black in last subplot are envelopes, i.e. |STFT|. Relevant here is

$$\cos(A) + \cos(B) = 2 \cos(.5(A - B)) \cos(.5(A + B)) \tag{1}$$

i.e. Sum of Sines $$\Leftrightarrow$$ A.M. Sine. Indeed in case of freq=128 we get such a sine since the symmetric filter is centered right between the unit impulses, and in freq=121 a watered down version of it. This wouldn't happen if the filters lacked negative frequencies. Results with x4 the n_fft (i.e. more filters):

The context here is of course signal amplitude from |STFT| - more fully, see STFT amplitude extraction criteria. Note that these proper real & imaginary parts are not reproducible with standard STFT implementations (scipy, librosa & others), covered here.

### Why it's not about resolution

The link to resolution is indirect, all windows (except flat) experience this: higher time resolution ($$\Rightarrow$$ higher bandwidth) just increases the range of qualifying frequencies. An extreme counter-example is instantaneous AM-FM localization of crossed hyperbolic chirps, which requires excellent time resolution - piece of cake for CWT with Generalized Morse Wavelets:

and that's with SSQ doing wavelet-correctional heavy lifting; plain CWT:

The STFT is not limited to the standard, column-wise formulation; a row-wise implementation (sample here) can force filter analyticity.

The picture is incomplete.

What matters more is the sinusoidal behavior under the envelope, rather than envelope size alone, of a filter. What's true is, under standard STFT the sinusoids start to misbehave as the peak frequency nears Nyquist, and this effect is worsened by narrower windows. As was shown with CWT however, high time resolution and analyticity are compatible - and for our purposes, CWT is just log-scaled STFT.

Example:

Left is a reference filter, right is same filter with greater bandwidth and forced analyticity. It's true that left enjoys a greater Heisenberg time resolution, but right has narrower main lobe. Unfortunately this is always true unless we take the filter on right to an extreme (practically useless if this filter is part of an otherwise low-bandwidth filterbank): forced analyticity suffers long tails, and this can be understood in terms of periods of constituent sinusoids in relation to window size.

We see one of required sinusoidal behaviors in this Nyquist-centered edge case: having an imaginary part, which gets progressively attenuated for a frequency-symmetric filter (always the case for real-valued STFT window) upon introduction of negative frequencies. There's also norms and real-imaginary symmetries, that I've explored here.

Correct. Though concerning the comments, this answer's points on resolution apply (esp. frequency which is also satisfied per Heisenberg).

### Animation

Full animation (epilepsy warning)

Another perspective is via sine STFT closed form solution (animation source; see under "Insights")