I'm coding the amplitude spectrum plot of a square wave signal with the following relation@


where I've already calculated the constant 𝛼 by taking the signal FFT and fitting the peaks. However, I'm having trouble figuring out what the amplitude is to plot in this case - any insight would be appreciated.

  • $\begingroup$ Can you give more details as to what trouble you are having? It would help if you could show what you did exactly, what results you get and from that what you mean by "having trouble". Are you also familiar with what "truth" should be given the Fourier Series expansion of a square wave? $\endgroup$ Dec 12, 2022 at 17:49
  • $\begingroup$ @DanBoschen The trouble is that I need to figure out what to plot for amplitude. Sorry, I'm not sure how to expand, I just don't know how to derive amplitude from the information I have. $\endgroup$
    – Navia
    Dec 12, 2022 at 17:53
  • $\begingroup$ Ah ok --- I think I understand your challenge and will try and provide an answer that may help you. Let me know if this is off track $\endgroup$ Dec 12, 2022 at 17:55

1 Answer 1


I believe the OP may be caught on the relationship between a peak in the complex FFT output and the amplitude of the related sinusoidal component in the time domain. Each bin in the FFT of a real signal has a magnitude that is directly proportional to the sinusoidal components in the time domain for the frequency of that bin. Typically the FFT of a real waveform with $N$ samples will also have a gain of $N$, (and for a sinusoid this gain is distributed in two components with $N/2$ in each, see details below) but this scaling is dependent on actual FFT implementation.

If the OP is extracting the relative gains for the harmonics of a square wave by fitting the peaks, then the magnitude of each complex result will be proportional to the amplitude for that particular frequency component. The true harmonics of a square wave will be the odd harmonics with an amplitude that goes down at $1/n$ where $n$ is the harmonic number.

The FFT can only approximate this true result since it is circular about the sampling rate: as the sampling rate is increased so that more or more of the "true" components are in the range of $f_s/2$, the closer the approximation will be.

Further details:

First, the FFT is an algorithm that computes the DFT, which is typically given as:

$$X[k] = \sum_{n=0}^{N-1}x[n]e^{-j\omega_o nk}$$

With the time domain reconstruction, or inverse DFT as:

$$x[n] = \frac{1}{N}\sum_{n=0}^{N-1}X[k]e^{j\omega_o nk}$$

Where we have $X[k]$ as each of the bins in the DFT result as a complex magnitude, giving the magnitude and phase for that particular frequency component at index $k$. The first DFT bin where $k=0$ is the "DC bin" and represents the DC component or average value of the time domain signal. The next DFT bin where $k=1$ represents what I will refer to as the fundamental frequency. This makes it very easy to understand the DFT by first understanding the principles of the Fourier Series Expansion (FSE): in the FSE, any arbitrary analytic waveform over time duration $T$ can be expressed as an infinite sum of sinusoidal components each as a multiple of the fundamental frequency, which is $1/T$ Hz.

In the DFT, the frequency components are given as complex phasors, notably samples of $e^{j \omega t}$. We recognize from Euler's identity for a cosine (and sine has a similar relationship) that a sinusoid is made up of two such phasors:

$$2\cos(\omega t) = e^{j \omega t} + e^{-j \omega t}$$

So notice how the $N$ samples in time of $x[n]$ and the sampling rate $f_s$ sets the total time duration $T$, as $T = N/f_s$. The "fundamental frequency" $\omega_o$ is therefore $2\pi f_o = 2\pi 1/T = 2\pi f_s/N$. It is common to express the DFT in terms of the normalized sampling rate where $f_s =1$ and with that we get the expression for the DFT as:

$$X[k] = \sum_{n=0}^{N-1}x[n]e^{-j2\pi nk/N}$$

and inverse DFT as:

$$x[n] = \frac{1}{N}\sum_{n=0}^{N-1}X[k]e^{j2\pi nk/N}$$

Also notice how the two tones (given by Euler's) for a sinusoidal components will actually appear in the DFT result: A sinusoid will have two exponential components at $\pm f$ where $f$ is the frequency of the sinusoid. For any sampled system, the frequency compoents in the first Nyquist zone which is from $\pm f_s/2$ will repeat periodically in all higher Nyquist zones. This means a single frequency component at $f$ will repeat at $Mf_s + f$ for all integers $M$. The DFT is giving us the frequencies from DC (at $f=0$) to nearly $f_s$ ($f_s$ is the periodic repetition of DC). So a sinusoid with frequency $f$, which then has components at $\pm f$ will appear in the DFT at $f$ and $f_s-f$.

From this we get the insight into the growth of $N$ (consider the DFT with a DC signal at the input where $x[n]=1$ for all samples and $k=0$, the above formula for the DFT simplifies to the sum of 1 over $N$ samples.), and why and how we would have the additional 1/2 factor given the Euler's relationship I provided.

For further understanding beyond this it is important to understand how "frequency leakage" occurs in the DFT, specifically when the sinusoidal components are not an integer sub-multiple of the sampling rate. I go into that further in this existing post.


Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.