Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Is the Fourier Transform or Series of a Square Wave more Accurate? (Read 39022 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Hi,

What is a more accurate representation of the frequency component of a square wave?  The Fourier Transform or a very large expanded Fourier Series. 



If you look at the above image, it shows you the relatively trivial Fourier Series expansion of a square wave.  It is the Frequency then a third of the 3rd harmonic, 5th of the fifth harmonic and so forth on to forever....

BUT....

If we do the Fourier TRANSFORM of the square wave we can use the picture below to visualize it:



So which one is correct?  Is a square wave made up of the summation of discontinuous frequencies at the base plus every other odd harmonic up to infinity OR does it have a continuous frequency spectrum with SOME frequency content between the harmonics?  Here is one more, but it is a Youtube video:

Square Wave Harmonics

So... in his video he shows how the harmonics should show up, demonstrates it on his scope using the generator, but then doesn't say anything about the apparent lower level harmonics at the even multiples?!? 

Lastly, if I go ahead and use Cool Edit (yes, it will always be Cool Edit for me) to generate a square wave and do the FFT on it using multiple windows I still see frequency content at the other harmonics. 

The reason I ask is this.  If I were to play two perfect square waves of two different frequencies.  Is the frequency content really only the odd harmonics of both signals, or would it be the addition of the two Sinc functions?

(This is not homework by the way...)






Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #1
A square wave doesn't have a Fourier transform, only a Fourier series. 

Remember series is for infinite signals,  transform is for finite.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #2
A square wave doesn't have a Fourier transform, only a Fourier series. 

Remember series is for infinite signals,  transform is for finite.


But a square pulse does have a transform which I sent the picture of?  And why does a DFT/FFT show frequency components outside of the odd harmonics?

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #3
A square wave doesn't have a Fourier transform, only a Fourier series. 

Remember series is for infinite signals,  transform is for finite.


But a square pulse does have a transform which I sent the picture of?


The square pulse is not a square wave.  But yes, if you have a finite length signal, it can have a Fourier transform.

And why does a DFT/FFT show frequency components outside of the odd harmonics?


Odd/even just depends on if the signal has odd/even symmetry.  If you align the square wave such that its even symmetric about the origin, it will have even components.  Otherwise it will have odd or else odd and even. 

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #4
If you place your square pulse symmetrically about zero time, it would have even symmetry and a purely real Fourier Transform. If not, it will have imaginary components too.

Also, to understand things clearly it helps to remove "DC" offset, so for a square wave, alternate between -1 and +1 instead of 0 and +1.

If you start with one cycle of the square wave, you'll notice the frequency peaks are broadened. As you add cycles before and after zero, you'll notice them narrow.

The same goes for a cosine wave that is windowed by a "boxcar function", i.e. abruptly turned on then off after a certain number of cycles. The spectral peaks are broadened more, the shorter the window function. This is actually measurable in physics, such as in lasers with high-speed optical modulators disrupting the continuous beam and creating short pulses. The optical spectral width can been seen to broaden in inverse proportion to the pulse length. The shorter the pulse, the broader the spectral width (usually measured by averaging numerous pulses of the same width).

This is a technical subject, but Wikipedia has some material that may help.
https://en.wikipedia.org/wiki/Window_function
Dynamic – the artist formerly known as DickD

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #5
This wiki page also has some information about the different transforms:

http://en.wikipedia.org/wiki/Fourier_analy...ourier_analysis

Note that there are 4 transforms defined depending on if the signal is finite in length (and therefore can be finite in energy), periodic (and therefore infinite in length and energy), and the two discrete variants of those cases, one of which is the DFT (the transform computed by the FFT algorithms).  None of these 4 equations is more accurate than the others, rather for most functions, at most one of these transforms will converge (and perhaps none will).  To understand why, note that the classic Fourier transform requires integrating to infinity, an integral which will not converge if the signal has infinite energy (as a square wave does).  Likewise the other domains impose their own restrictions (hence you cannot take the FS of an aperiodic signal).

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #6
This wiki page also has some information about the different transforms:

http://en.wikipedia.org/wiki/Fourier_analy...ourier_analysis

Note that there are 4 transforms defined depending on if the signal is finite in length (and therefore can be finite in energy), periodic (and therefore infinite in length and energy), and the two discrete variants of those cases, one of which is the DFT (the transform computed by the FFT algorithms).  None of these 4 equations is more accurate than the others, rather for most functions, at most one of these transforms will converge (and perhaps none will).  To understand why, note that the classic Fourier transform requires integrating to infinity, an integral which will not converge if the signal has infinite energy (as a square wave does).  Likewise the other domains impose their own restrictions (hence you cannot take the FS of an aperiodic signal).


Let me try it this way.... If I have a THREE FINITE square waves over the same duration with one at 500 Hz, 1100 Hz and 2000 Hz what will be the proper way to determine the frequency content of the resulting signal.  Is it:

A:  1 at 500 Hz,  1/3 at 1,500 Hz,  1/5 at 2,500 Hz, 1/7 at 3,500 Hz, ... + 1 at 1,100 Hz, 1/3 at 3,300 Hz, 1/5 at 5,500 Hz, 1/7 at 7,700 Hz,... + 1 at 2,000 Hz, 1/3 at 6,000 Hz, 1/5 at 10,000 Hz, 1/7 at 14,000 Hz, ....

OR

B:  Sinc(500) + Sinc(1100) + Sinc(2000)

If I extend the Fourier series out to infinity it will only ever tell me that I have frequency content at the harmonics, but the other will give me broad frequency spectrum content.

If it is "windowing" then does that mean that the ups and downs offset to converge at only the harmonics????

I guess what I want to know is how would I create an equivalent DFT/FFT plot for an ideal square wave over a fixed interval knowing only the frequency?

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #7
A square wave doesn't have a Fourier transform, only a Fourier series. 

Remember series is for infinite signals,  transform is for finite.


You meant the DFT.
There's no reason you couldn't take the Fourier transform of a square wave. In fact, you can take the Fourier Series first and then take the Fourier Transform of all those exponential terms, resulting in a series of impulse fuctions. Then again, it will be a periodic function and you can take the Fourier Series of it, resulting in another series of complex exponentials.
Not that it matters much in practice.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #8
Let me try it this way.... If I have a THREE FINITE square waves


What is a finite square wave?  The usual definition is infinite.  If you want to use special functions, it might make sense to just give the equations.

A square wave doesn't have a Fourier transform, only a Fourier series. 

Remember series is for infinite signals,  transform is for finite.


You meant the DFT.


No, the DFT is for discrete periodic signals. 

There's no reason you couldn't take the Fourier transform of a square wave.


You can try, but its not defined because the function isn't square integrable.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #9
Everybody understands much more of this than I do, so I will start all the way over...

Let's start with a sampled signal of a duration of 1/240 seconds at a rate of 61440 samples per second.  That signal will be a discrete signal consisting of 256 individual amplitudes representing the voltage, let's pretend we did it at 8 bit to make the numbers easy, and let's do it unsigned, so those values are between -127 and 128.  Take that signal and run it through a discrete fourier transform at 256 and you will get a series of dB levels by frequencies over a fixed interval.  Take a look at the image below to see just such a signal.

First Wave

You can see it has quite a bit of content at 1,200 Hz, 6,000 Hz, 11,000, 18,000 Hz and 30,000 Hz.  So - I know if I could play five sine waves at the same time that I could get a whole lot of the proper frequencies in the signal.  You can see what it looks like when I generate that signal (using equal amplitude for each) and when I play it my ear hears ALMOST the same thing, but much more "clean."

Second Wave

So, now if I take the three important frequencies, call them the 1.2k, the 6k and 18k then try the same with a square wave.  We get:

Third Wave

As you can see the frequency content is much more rich based on this, and what I would like to do is given a non-periodic discrete signal how can I determine the best three square wave frequencies to represent that signal.  I know I will lose information.  I know I won't get an exact duplication - but that is ok with me.  If I can get the frequency content close then I am fine.

My first thought was to do an FFT of the discrete signal, take the highest three frequencies and then use those, but the resulting signal has the harmonics which throw it off by a lot.  So, now I want to optimize with consideration of the harmonics and try to find the best three square wave frequencies.

So, do I just assume that all there is are sine waves at the odd harmonics or can I somehow reconstruct what the equivalent FFT would be so that I can better match the target signal?

Thoughts?




Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #10
You should increase your window length so that you can see what's going on in those dfts.

But I would not use harmonic analysis here. For such a simple waveform just numerically solve for the optimal square fit.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #11
You should increase your window length so that you can see what's going on in those dfts.

But I would not use harmonic analysis here. For such a simple waveform just numerically solve for the optimal square fit.


I'll be writing a program to do the conversion.  The wave form will not always be so simple.  Are you suggesting that I do it in the time domain?

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #12
A square wave doesn't have a Fourier transform, only a Fourier series.


A single sine wave has a Fourier transform consisting of two dirac delta functions, one each at plus and minus the sine wave's frequency.  Using the linearity property of the Fourier transform, combined with the Fourier series of the square wave, one could compute the Fourier transform of the square wave as a train of delta functions.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #13
A square wave doesn't have a Fourier transform, only a Fourier series.


A single sine wave has a Fourier transform consisting of two dirac delta functions, one each at plus and minus the sine wave's frequency.  Using the linearity property of the Fourier transform, combined with the Fourier series of the square wave, one could compute the Fourier transform of the square wave as a train of delta functions.


This is splitting hairs, but I'd argue you're not computing it, you're just defining it that way for convenience by using a special 'function'.  In reality the Fourier integral doesn't converge, and the dirac isn't even a well defined function, its an abstraction (actually wikipedia says distribution) used to make the Fourier transform agree with the Fourier series.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #14
I'll be writing a program to do the conversion.  The wave form will not always be so simple.  Are you suggesting that I do it in the time domain?


Your problem seems to be a sparse approximation problem, and it might be better solved with a matching pursuit-alike approach than with the spectral analysis.

Assuming the signal you're trying to decompose is s(t), and the basis function is sq(t, f) = sign(sin(f*t)), search for the first component by finding the frequency which maximizes the integral of product s(t)*sq(t, f) over the defined time window. You can use one of the ready-made optimization algorithms for this, like Nelder-Mead for example. Once the first optimal frequency f1 is found, subtract the sq(t, f1) from your signal s(t), then repeat the process on the residual, and so on until the required number of frequencies are found (or until the maximum integral value becomes close to zero).

BTW, are you only allowed to vary the frequency? Because optimizing for the square waves phase and magnitude as well might give a better approximation.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #15
I'll be writing a program to do the conversion.  The wave form will not always be so simple.  Are you suggesting that I do it in the time domain?


Your problem seems to be a sparse approximation problem, and it might be better solved with a matching pursuit-alike approach than with the spectral analysis.

Assuming the signal you're trying to decompose is s(t), and the basis function is sq(t, f) = sign(sin(f*t)), search for the first component by finding the frequency which maximizes the integral of product s(t)*sq(t, f) over the defined time window. You can use one of the ready-made optimization algorithms for this, like Nelder-Mead for example. Once the first optimal frequency f1 is found, subtract the sq(t, f1) from your signal s(t), then repeat the process on the residual, and so on until the required number of frequencies are found (or until the maximum integral value becomes close to zero).

BTW, are you only allowed to vary the frequency? Because optimizing for the square waves phase and magnitude as well might give a better approximation.


Wow... so this is highly relevant.  I have been thinking about how to build a wavelet model, and then mentally reverted back to matching the spectral analysis.  What you describe above seems almost too good to be true.  To clarify, in fact, my plan was to model using perfect square waves at a fixed magnitude.  I can't do anything about the phase.  In reality, because it is a real device generating the square wave, as the frequency gets higher the magnitude goes down as the signal cannot get to the highest level prior to the polarity switching, so, in reality my real basis function (which I think I can figure out) would need a factor proportional to some constant over f so (c/f)*sq(t,f).  I was going to ignore the impact of the lower magnitudes on the higher frequencies, but not sure that I should do that.  Wow... my head is spinning because this seems so much more like the proper way to approach this.  You are really smart!

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #16
This is splitting hairs, but I'd argue you're not computing it, you're just defining it that way for convenience by using a special 'function'.  In reality the Fourier integral doesn't converge, and the dirac isn't even a well defined function, its an abstraction (actually wikipedia says distribution) used to make the Fourier transform agree with the Fourier series.


Since we are already splitting hairs: there is no need for the delta "function" to be a well-defined "function". Just turn to measures instead, and lucky as you are, it turns out there is a good old-fashioned function to represent the same thing, namely the Heaviside step.
http://en.wikipedia.org/wiki/Fourier_trans...ltjes_transform is [arguably I cheat here] a unification of the discrete Fourier transform and the (absolutely) continuous one. You might need to inverse-transform a measure then, but that is just a technical annoyance: probabilists do it all the time.

(The probabilists' transform is E[exp(iuX)] - no minus sign and no 2? -  to transform a random variable X or equivalently, its probability measure P. Whether or not X resp. P has a density. http://en.wikipedia.org/wiki/Fourier_trans...her_conventions - for feinschmeckers, probabilists and physicists use different Hermite functions for basis too.)

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #17
In reality, because it is a real device generating the square wave, as the frequency gets higher the magnitude goes down as the signal cannot get to the highest level prior to the polarity switching


In which case it's also not so square anymore. Which means that a more correct model may be needed, like a square wave filtered with a sloping low-pass filter. Or, if you have access to the actual device, you could record its output at different frequencies and create a dictionary of digitized waveforms to match to.

Quote
my head is spinning because this seems so much more like the proper way to approach this.


Don't get too excited, I never said this method is guaranteed to work  But It wouldn't hurt to try.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #18
This is splitting hairs, but I'd argue you're not computing it, you're just defining it that way for convenience by using a special 'function'.  In reality the Fourier integral doesn't converge, and the dirac isn't even a well defined function, its an abstraction (actually wikipedia says distribution) used to make the Fourier transform agree with the Fourier series.


Since we are already splitting hairs: there is no need for the delta "function" to be a well-defined "function". Just turn to measures instead, and lucky as you are, it turns out there is a good old-fashioned function to represent the same thing, namely the Heaviside step.
http://en.wikipedia.org/wiki/Fourier_trans...ltjes_transform is [arguably I cheat here] a unification of the discrete Fourier transform and the (absolutely) continuous one. You might need to inverse-transform a measure then, but that is just a technical annoyance: probabilists do it all the time.


I don't know enough to disagree with that math, but defining the Fourier transform of an infinite waveform to be two deltas is problematic because it turns a function with infinite energy into one with finite energy, and so violates Parseval's theorem.  My original point was that one should use Fourier series for periodic functions, and Fourier Transforms for finite functions, because the series is defined over one period and therefore will preserve average power (energy per cycle) while the transform is integrated over the entire function and will therefore preserve energy.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #19
In reality, because it is a real device generating the square wave, as the frequency gets higher the magnitude goes down as the signal cannot get to the highest level prior to the polarity switching


In which case it's also not so square anymore. Which means that a more correct model may be needed, like a square wave filtered with a sloping low-pass filter. Or, if you have access to the actual device, you could record its output at different frequencies and create a dictionary of digitized waveforms to match to.

Quote
my head is spinning because this seems so much more like the proper way to approach this.


Don't get too excited, I never said this method is guaranteed to work  But It wouldn't hurt to try.


You should know that the method is working, and of course the output isn't great but certainly recognizable.  I'll post real recorded results when I am happy with them, but it is resulting an effective compression ratio of 99.5% - or representing the sound wave using 480 bytes per second instead of 96,000 for a 16 bit mono 48k sample rate.  Shocked that it is working as well as it is.  For right now I've just picked ONE square wave to represent the target signal.  Lots more to do - but wow so far...

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #20
I'm late to the thread but I'll add my two bits anyways.
Remember series is for infinite signals,  transform is for finite.

That's the opposite of true.

The fourier series is defined for finite signals, and for periodic signals of period tau by considering them as functions on R mod (tau) ~=[0,tau]. It is not defined for infinite nonperiodic signals.

The natural setting for the fourier transform is the space of tempered distributions - a space which includes all bounded functions, unbounded functions which grow "slowly enough," and loads of distributions which aren't functions, including the finite sums of dirac distributions mentioned earlier. The fourier transform is a normal well behaved function for any function in L^p for 1<=p<=2, even if it has infinite support; famously, the fourier transform of the normal distribution is just the normal distribution (this fact is closely related to a direct proof of the central limit theorem).

Your claim that fourier transforms which are distributions and not functions are just some ad hoc way of defining things is not "splitting hairs," it's wrong. If you study any vector space you really need to also study its dual space to get anywhere, and for many natural or simple function spaces that means studying expansive dual spaces of distributions; the fourier transform still helps in studying those function spaces. And we didn't just "define" the fourier transform of sin(x) to be the sum of two dirac distributions, as though we could have "defined" it any way we pleased. We computed its transform in the only consistent way possible.

Parseval's theorem says that the fourier transform is a unitary operator on L^2 (and therefore an L^2 isometry, which is what you mean by "energy preserving"). The fourier transform isn't "problematic" on spaces other than L^2, it's just that Parseval's theorem doesn't tell us about any distributions other than square integrable functions or about any norms except for the L^2 norm.

Of course neither sin(x) nor the dirac delta has an energy (a L^2 norm). The natural way to answer whether these have "infinite energy" shows that the "energy" of a dirac distribution is just as infinite as that of sin(x). The dirac distribution is the limit of sequences of functions whose norm in any L^p except for p=1 goes to infinity, while their L^1 norm is 1 (e.g. tent functions with height n and width 2/n, normal distributions of variance 1/n, or n*sinc(n*x)/pi). Similarly, sin(x) is the limit of sequences of functions whose norm in any L^p except for p=infinity goes to infinity, while their L^infinity norm is 1 (e.g. the functions sin(x)*indicator([-n,n])). So they both have "infinite energy."

For periodic signals the Fourier transform is just a sum of dirac distributions at frequencies which are integer multiples of the basic frequency 2pi/period, and the coefficients in that sum are the coefficients of the Fourier series. So the fourier transform of a periodic signal does the same thing the series does, except you can take the transform without knowing the period ahead of time.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #21
I'm late to the thread but I'll add my two bits anyways.
Remember series is for infinite signals,  transform is for finite.

That's the opposite of true.

The fourier series is defined for finite signals, and for periodic signals of period tau by considering them as functions on R mod (tau) ~=[0,tau].



Thats not the opposite of true, since you just said its defined for infinite periodic functions.  Anyway, which aperiodic functions are you thinking of that have a fourier series expansion?  Not saying you are wrong, I just don't know of any and I'm not sure how you'd make that series work. 

Parseval's theorem says that the fourier transform is a unitary operator on L^2 (and therefore an L^2 isometry, which is what you mean by "energy preserving"). The fourier transform isn't "problematic" on spaces other than L^2, it's just that Parseval's theorem doesn't tell us about any distributions other than square integrable functions or about any norms except for the L^2 norm.


Which is problematic if we wish to apply them to acoustic signals which, we expect, ought to conserve energy.

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #22
It's not some special collection of finite functions which have fourier series; given any finite interval and any function whatsoever on that interval, we can easily compute its Fourier series. If the function is continuous and takes equal values at the endpoints of the interval, the fourier series of the function equals the function on that interval. This is where the Fourier series came from in the first place: solving the heat equation on finite intervals.

The Fourier series can only use information from a finite interval. Saying "it applies to infinite signals as long as they're periodic" is like "you can go anywhere in the universe you like as long as the only place in the universe you like is that chair" - all we're doing is copying and pasting our finite signal. The fact that if f=g on [0,1) then the periodic extension of f equals the periodic extension of g is not some new and miraculous application to infinite domains.

Which is problematic if we wish to apply them to acoustic signals which, we expect, ought to conserve energy.
Again, sin(x) doesn't have an energy since it's not square integrable. If you want to talk about it having an infinite energy, then so does its fourier transform which is the sum of two deltas. And I don't have a clue what you're getting at by claiming that there's something special about acoustic signals, the fourier transform of acoustic signals, or your "expectations."

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #23
It's not some special collection of finite functions which have fourier series; given any finite interval and any function whatsoever on that interval, we can easily compute its Fourier series.


Yes, but as I said above, and you disagreed with, if the function isn't finite, you have a problem. 

The Fourier series can only use information from a finite interval.


Which is another way of saying that it does not apply to finite functions ...

Again, sin(x) doesn't have an energy since it's not square integrable.


Hence my suggestion that the Fourier series is more appropriate for analyzing it. 

Is the Fourier Transform or Series of a Square Wave more Accurate?

Reply #24
It's not some special collection of finite functions which have fourier series; given any finite interval and any function whatsoever on that interval, we can easily compute its Fourier series.
Yes, but as I said above, and you disagreed with, if the function isn't finite, you have a problem.
No. What you said above is that the fourier transform has a problem if the function isn't finite, which is precisely the opposite of correct. The Fourier series has a problem, which is the entire point of why the fourier transform was invented in the first place- to extend the idea of trigonometric representation to functions defined on infinite domains (so as to solve the heat equation for infinite initial conditions). You can only take the fourier transform of a finite signal by considering it to be an infinite signal which just happens to have compact support. And the fourier transform of a nonzero function with compact support will not have compact support, e.g. rectangle<->sinc.
The Fourier series can only use information from a finite interval.
Which is another way of saying that it does not apply to finite functions ...
What???? That's nonsense. As I said above, the more precise way to put this is that periodic functions aren't infinite in the relevant sense, they're just functions on the set R mod (tau) which is (isomorphic to) the finite interval [0,tau] with endpoints identified (equivalently, a circle of circumference tau). And we have to know the period before we can compute a fourier series (if you calculate a fourier series using a period which isn't a period of the function then the fourier series will equal the function on that interval but not outside it), so there is no sense in which we're looking at any more than that slice of the real line.

Again, sin(x) doesn't have an energy since it's not square integrable.
Hence my suggestion that the Fourier series is more appropriate for analyzing it.
That's silly. Again, if a function is periodic and we know its period beforehand, the fourier series and the transform accomplish exactly the same thing; the fourier series is just the coefficients of the dirac comb which is the transform. Neither is more appropriate than the other in such a case and square integrability has nothing to do with it.