# FFT of triangular wave

I am new in dsp.. so please bear with me.

I want to plot FFT of a triangular wave. I am using the code below to first generate the triangular wave and then take its FFT


Fs=50e6;
Ts=1/Fs;
NFFT=2^14;
Runtime=(NFFT-1)*Ts;
t=0:Ts:Runtime;

fin=5*1e6;

factor=Fs/NFFT;
bin=round(fin/factor);
fin=bin*factor;

fin_MHz=fin/1e6;

y_in=sawtooth(2*pi*fin*t,0.5);

% Calculate Spectrum
Y = fft(y_in,NFFT)/NFFT;
f = (Fs/1e6)*linspace(0,1,NFFT);

Ydb = 20*log10(abs(Y(1:NFFT))) + 6.02;

% % Plot amplitude spectrum.

plot(f,Ydb)
hold on
grid on
ylim([-150 5])
xlim([-5 55])
xlabel('Frequency (MHz)')
ylabel('dB')
set(gca,'YTick',-150:25:0)
refline(0,-78)



The response when input is 5 MHz is as follows :

The response when input is 5.5 MHz is as follows :

My questions are :

1. Do i have any mistakes in my code ?

2. The response should have been sinc^2 .. i dont see it

3. Why the difference between two plots.. i mean more spurs when ip is 5.5 mhz

4. The amp peak should be in 1 in fft .. it -1.825 dbfs .. ? why ?

Thankyou so much.

Do i have any mistakes in my code ?

Probably OK.

The response should have been sinc^2 .. i dont see it

Fourier Series of Triangle Wave

Why the difference between two plots.. i mean more spurs when ip is 5.5 mhz

Because of spectral aliasing. For 5MHz triangular wave, the harmonics are:

5, 15, 25, 35(-15), 45(-5), 55(5), ...


For 5.5MHz triangular wave, the harmonics are:

5.5, 16.5, 27.5(-22.5), 38.5(-11.5), 49.5(-0.5), 60.5(10.5), ...


On 5MHz triangular wave, the spectral aliasing is hidden because it coincides with non-aliasing spectrum. But, it seems that your code changes the frequency to be multiple of Fs/NFFT, so it is not perfectly hidden.

For perfectly hidden spectral aliasing here, the frequency should be power of two multiple of Fs/NFFT. For example: 3.125 MHz.

The amp peak should be in 1 in fft .. it -1.825 dbfs .. ? why ?

Based on the fourier series, it is $$\frac{8}{\pi^2}$$

Do i have any mistakes in my code ?

Code seems clean. I would instead replace

% Calculate Spectrum
Y = fft(y_in,NFFT)/NFFT;
f = (Fs/1e6)*linspace(-0.5,0.5,NFFT); % it is better to center your spectrum

Ydb = 20*log10(abs(Y(1:NFFT))) + 6.02;

% % Plot amplitude spectrum.

plot(f,Ydb)
hold on
grid on

xlabel('Frequency (MHz)')
ylabel('dB')
set(gca,'YTick',-150:25:0)


The response should have been $$\text{sinc}^2$$ .. i dont see it

It's not a sinc, per say. According to Fourier series, you can write a sawtooth wave as $$x(t) = - \frac{A}{\pi}\sum_{n=1}^{\infty} \frac{1}{n} \sin n \omega t$$ To verify plot

plot(real(Y(1:NFFT)))


Why the difference between two plots.. i mean more spurs when ip is 5.5 mhz

Note that the spectral information is in $$[-f_{in},f_{in}]$$. The FFT will display you information in $$[-\frac{F_s}{2},\frac{F_s}{2}]$$ in discritized bins. Therefore, anything outside the band of interest [-5,5] MHz serves as a replica.

The amp peak should be in 1 in fft .. it -1.825 dbfs .. ? why ?

I don't quite get what you mean here.