LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

FFT on triangular and square waves.

Hi,
 
 
I have been computing FFTs on a some sinusoidal waves and have been doing fine, gett the right Y axis values in the units Vrms^2.  For example a 10V amplitude (20v peak to peak) wave at 100 htz would give a peak at 100 htz and 49Vrms^2 since the RMS value fo 10 is 7 and 7^2 is 49.  Now when I input a Square wave with the same characteristics I no longer get the same scaling instead getting a peak at a higher y value.  With a triangular wave I get peaks at lower values.  Can anyone explain to me why this is happening?
 
Thanks.
Intern NSWCCD Carderock.
0 Kudos
Message 1 of 12
(10,548 Views)
Square waves produce lots of harmonics.  A square wave can be thought of as the sum of many sinewaves at different frequencies.  These frequencies get mixed together and produce other frequencies, like the sum of F1 + F2, the difference of F1 - F2, and intermodulation products (2*F1 - F2) and so on.  Since there are many frequencies produced by a square wave, an FFT or a spectral analysis will show lots of peaks at different frequencies.  That is the nature of square waves.  Don't expect to just see one peak at the actual fundamental square wave frequency.  Look at it this way, a square wave has a very fast rise and fall time.  A sine wave of a much higher frequency will also have a very fast rise and fall time.  That higher frequency is one component of the square wave.  Take all the points on the flat top portion of the square wave.  Each one of those points can be produced by the peak value of a sine wave at a higher frequency.  If you add all those sine waves and limit their amplitude, you will get a square wave.  Since the FFT transforms a signal into the frequency domain, you get lots of peaks at different frequencies.
Triangle waves are similar in that they are also a sum of many sinewaves.  Since the rise time of a triangle wave is less than a sine wave of the same frequency, the components of a triangle wave will be sinewaves of lower frequencies.  FFT shows the frequency domain, and the frequencies shown are the frequencies of sine waves.  That is the nature of FFT and spectral analysis.  Only sine waves are pure frequencies.  Look at any mathematical formula for waveforms, they all include sine or cosine.  There is no mathematical function for square or triangle.  Therfore square and triangle waves have to be expressed by sums and/or differences of multiple sine waves.   I hope that this is clear to you.  If not, ask again.
- tbob

Inventor of the WORM Global
Message 2 of 12
(10,531 Views)
Thanks a lot.  It makes sense now.
Intern NSWCCD Carderock.
0 Kudos
Message 3 of 12
(10,522 Views)
This subject is very interesting to me. I generated square wave and put it through BP filter and LP filter (2'nd order).
I found that output from LP filter is sth, like sine wave sumed with square wave of lower freq and amplitude.
When I run through BP filter with LF=source freq, HF=source freqx1,1 I got something like sine aumed with triangle
When I change the LF=0.5 source freq in LP filter  the output is much less sum of two sinusoids.
Looks to me like butterworth filter (which I'm using) behaves, like ADC converter, which adds sampling frequency harmonics


And I wanted just only to simulate signal output after passing square through simple rc filter (and the LC filter)
0 Kudos
Message 4 of 12
(10,450 Views)
Here is a little program (v 7.1) that allows you to see what happens when you remove some harmonics from a square wave.  The cursors in the FFT plot can be moved to select only the harmonics between them.  The resulting waveform is then shown.
Randall Pursley
Message 5 of 12
(10,439 Views)
thanks for the code. it's very informative and acc. to theory. But if You cosider post from tbob there should be some lower harmonics as well ( intermodulation harmonics) what You don't have.
 It now appears to me that I don't understand sqare waveform.vi or LV in general
If I want continously generate square wave which I send sample by sample to DAQ AO, can I put Your sample into subvi.
I think not, because your example just send time limited wave.
this is what I did on below gif.
You may see the square wave is not 50% duty cycle as supposed to be.
I don't know if fs is 3584HZ and number of samples is 128 in the waveform, should I call  squareWaveform.vi every 128/fs time, get array fo samples and then send them one by one to AO????

0 Kudos
Message 6 of 12
(10,427 Views)
I forgot to mention, that I can not send square wave to AO from constant circular buffer, because my real output would be sum of three square waves of different frequencies and phases. Then I would like to see FFT of such signal  coming to AI.It's a kind of modulation of one sqare wave by another one (or other ones)
0 Kudos
Message 7 of 12
(10,425 Views)

Intermodulation products occur during non-ideal multiplication due to non-linearities.  In software where ideal signals can be used you will not see any intermodulation products.  In hardware you see them all of the time due to non-linearities in the mixers which produce them in addition to the desired multiplication (mixing).


Randall Pursley
Message 8 of 12
(10,411 Views)

Actually RMS (Root Mean Square) value of periodic signal f(t) is defined as a square root of squared signal averaged over the period T, mathematically:

 

f_rms = sqrt ( aver (f^2) ),

where aver (f^2) = [integral of f^2 over the period T] / T

 

It is easy to show that for pure sine wave f(t) = A*sin(2*pi*t / T), f_rms = A / sqrt(2) ~ 0.707*A – this is well-known result anyone can find in almost every textbook. It is not difficult to calculate exact value of f_rms for square and triangular waves. Results are as follows:

 

f_rms (square) = A, where square wave f(t) = A, if 0<t<c*T and f(t) = -A, if c*T<t<T (here 0<c<1 and c=0.5 for 50% duty cycle wave)

and

f_rms (triangle) = A / srqt(3) ~ 0.577*A, where I assumed triangle wave that raises from 0 to +A over 0 < t <T/4, goes down to –A over T/4 < t < 3T/4 and returns to ZERO in the last quarter of the period T

 

Parseval’s theorem (http://en.wikipedia.org/wiki/Parseval's_theorem) of Fourier analysis states that energy of periodic signal (=integral of f^2 over the period T) must equal to energy of its Fourier transform ( sum of squared amplitude of all harmonics for periodic signal). That’s why Fourier analysis in Labview is capable of producing signal’s RMS value in addition to its spectrum.

 

Now, main harmonic of square and triangular waves carries majority of signal energy, but not all of it because of presence of other harmonics. That means that amplitude of main harmonic as reported by Labview will be close to but slightly less than signal’s RMS value. This explains the results you reported in your very first post.

0 Kudos
Message 9 of 12
(10,410 Views)
To get this to work and address the duty cycle problem, your sampling frequency would have to be a common multiple of the three square waves you are generating (i.e. 1 kHz, 1.1 kHz, 1.2 kHz would require a fs of at least 132 kHz, this gives you 132 points/cycle of 1 kHz, 120 points/cycle for the 1.1 kHz, and 110 points/cycle for the 1.2 kHz, all even numbers of points).

There will be some number of points that will make a complete cycle of your summed waveform.  You will have to figure out what that is (in my example the waveform would repeat after 10 cycles of 1 kHz = 11 cycles of 1.1 kHz = 12 cycles of 1.2 kHz or 10 ms).  You can upload that specific number of points to continuously output to the AO.

As for the FFT of the result, since you are summing waveforms, it will be the sum of the FFTs of the three waveforms you are summing together so if you know what signals you are putting out, you already know what the FFT of the resulting signal should look like.
Randall Pursley
Message 10 of 12
(10,407 Views)