### Author Topic: Reconstructing a waveform from its harmonics.  (Read 5284 times)

0 Members and 1 Guest are viewing this topic.

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #25 on: March 10, 2018, 06:16:49 am »
I just added a plot table of the data in column 'B' as shown in my screenshot.  It is clearly a perfectly constructed textbook example waveform from the Fourier data.  If Simon does the assignment Inverse Fourier correct as expected by the course, he should be able to reconstruct the red waveform 'EXACTLY' down to the dot, there can be no error here, phase mistakes, or excuses.

Yes they show the wave on the previous sheet of the assignment. But from your expanded image of the spectrum there are more harmonics nearer 18 KHz. I'm not sure why there is a partial waveform on the end of the data, from what I read the section of data used for analysis should be one complete cycle.
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

#### rstofer

• Super Contributor
• Posts: 5299
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #26 on: March 10, 2018, 06:50:04 am »
I just added a plot table of the data in column 'B' as shown in my screenshot.

Duh!  I really missed that!

Sure, if you have f(t) given and you want f(t) back, just plot f(t).

I plotted the data in Matlab.  That is a lot harder than it looks because I first had to do some text editing on the values I copied from Excel.  Ultimately, I wound up with one 7k char line.  I just couldn't come to grips with breaking it up on continuation lines.  Clearly, Excel is the way to plot an Excel column.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #27 on: March 10, 2018, 07:17:16 am »
There is a clue on sample 512.  It is the only row with 1 Fourier component, and, a perfect frequency of 9000hz on the dot.  Magnitude column (H) and column (I) all begin running backwards from that point on the the bottom.  Only samples 1 through 511 are valid Fourier components. over the long run. Example:

sample row 1023 H&I = sample row 1 H&I
sample row 1022 H&I = sample row 2 H&I
sample row 1021 H&I = sample row 3 H&I
sample row 1020 H&I = sample row 4 H&I
Everything merges at row 511 and 513.  The Fourier components (D...G) are really close.

Though, the sample interval (A), sample (B), and frequency (J) continue unaffected.

The final 3 peaks at 18Khz may be the fact that the sample data goes from sample #1023 = -53.50 to 0 (ie no data left) after that point.  See photo: This does not seem to be correct since if you use these higher Fourier components, your re-construction will have a high frequency waveform throughout it, and a strong visible one at that.

Your sample rate is 18000hz.  This means the maximum possible frequency in the sample is 9000.  The higher peaks are like what you see in an oversampling ADC/DAC filter response above 1/2 sampling frequency.  Peaks exist, but they aren't giving meaningful information about the waveform except for single transition step/pulses.
« Last Edit: March 10, 2018, 07:23:33 am by BrianHG »
__________
BrianHG.

#### rstofer

• Super Contributor
• Posts: 5299
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #28 on: March 10, 2018, 07:22:46 am »
Info re: Excel and Fourier are all over the Internet and it includes a video.  The inverse transform is supported.

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #29 on: March 10, 2018, 07:25:30 am »
I'm going to have to reread the section of the module on FFT and do some more reading
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

#### mikerj

• Super Contributor
• Posts: 1714
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #30 on: March 10, 2018, 07:46:57 am »
I just added a plot table of the data in column 'B' as shown in my screenshot.  It is clearly a perfectly constructed textbook example waveform from the Fourier data.  If Simon does the assignment Inverse Fourier correct as expected by the course, he should be able to reconstruct the red waveform 'EXACTLY' down to the dot, there can be no error here, phase mistakes, or excuses.

The course paper says nothing about using an inverse Fourier.  It actually says reconstruct using the "principal harmonics" and even suggests sketching the waveforms which is clearly not going to exactly recreate the original.  Using the frequency, magnitude and phase of the three peaks (i.e. fundamental and two harmonics) gives a waveform similar to, but not exactly the same as the original as would be expected and is shown below.

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #31 on: March 10, 2018, 07:57:45 am »
I just added a plot table of the data in column 'B' as shown in my screenshot.  It is clearly a perfectly constructed textbook example waveform from the Fourier data.  If Simon does the assignment Inverse Fourier correct as expected by the course, he should be able to reconstruct the red waveform 'EXACTLY' down to the dot, there can be no error here, phase mistakes, or excuses.

The course paper says nothing about using an inverse Fourier.  It actually says reconstruct using the "principal harmonics" and even suggests sketching the waveforms which is clearly not going to exactly recreate the original.  Using the frequency, magnitude and phase of the three peaks (i.e. fundamental and two harmonics) gives a waveform similar to, but not exactly the same as the original as would be expected and is shown below.

Which is what I am attempting although the second and third peaks are not actually harmonics of the first as they are not an exact multiple. As pointed out earlier I need to establish the phase angle in order to get it right. if I don't I get the attached. I'll be delving into this tomorrow.
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #32 on: March 10, 2018, 08:04:21 am »
the thing that is really confusing is the harmonic distortion as like I said the second and third peaks are not harmonics of the first although if I assume they are I get a distortion of about 90%, if I simply use all the values in one cycle I get over 800% which surely is not possible. if I use 52.73 as the fundamental and go every 4 squares (multiples of 52.73) I get again around 90% even though I'm not using the values of the second and third peaks.
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

#### Vtile

• Frequent Contributor
• Posts: 953
• Country:
• Ingineer
##### Re: Reconstructing a waveform from its harmonics.
« Reply #33 on: March 10, 2018, 08:30:56 am »
From another thread if it gives any 'heureka' moments.

#### rhb

• Super Contributor
• Posts: 2065
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #34 on: March 10, 2018, 08:56:55 am »
In addition to the smearing from only having 1024 samples, there is wraparound because the time domain data were not zero padded to 2048 samples before computing the FFT.  This is immediately evident in the reconstruction shown.

#### Jay_Diddy_B

• Super Contributor
• Posts: 1683
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #35 on: March 10, 2018, 09:17:54 am »
Hi,

You can also do some simple experiments in SPICE.

Consider this model:

I have two voltage source in series. I have a fundamental frequency 1kHz and third harmonic 3kHz. The amplitude of the 3rd harmonic is 1/9 the amplitude of the fundamental. The difference between the two is the phase relationship of the harmonic to the fundamental.

If you look at the time domain waveforms you see that they are different:

However if you look at the FFT, they are they same. There should only be two components, the fundamental at 1khz and the harmonic at 3kHz. Since the phase information is not included in the FFT you can not reconstruct the original waveforms from the FFT. The phase relationship is needed and it is missing.

The other FFT components are from the sampling window etc.

Regards,

Jay_Diddy_B

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #36 on: March 10, 2018, 10:37:34 am »
I just added a plot table of the data in column 'B' as shown in my screenshot.  It is clearly a perfectly constructed textbook example waveform from the Fourier data.  If Simon does the assignment Inverse Fourier correct as expected by the course, he should be able to reconstruct the red waveform 'EXACTLY' down to the dot, there can be no error here, phase mistakes, or excuses.

The course paper says nothing about using an inverse Fourier.  It actually says reconstruct using the "principal harmonics" and even suggests sketching the waveforms which is clearly not going to exactly recreate the original.  Using the frequency, magnitude and phase of the three peaks (i.e. fundamental and two harmonics) gives a waveform similar to, but not exactly the same as the original as would be expected and is shown below.

Which is what I am attempting although the second and third peaks are not actually harmonics of the first as they are not an exact multiple. As pointed out earlier I need to establish the phase angle in order to get it right. if I don't I get the attached. I'll be delving into this tomorrow.

Can you share with us how you got this result.
__________
BrianHG.

#### rhb

• Super Contributor
• Posts: 2065
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #37 on: March 10, 2018, 11:15:48 am »

However if you look at the FFT, they are they same. There should only be two components, the fundamental at 1khz and the harmonic at 3kHz. Since the phase information is not included in the FFT you can not reconstruct the original waveforms from the FFT. The phase relationship is needed and it is missing.

I'm sorry, but that statement is wrong.  What is being shown is the amplitude spectrum, NOT the FFT.   That is, if  Jay_Diddy_B actually calculated the modulus instead of just taking the real part and plotting it.  In thirty years of heavy DSP work I've never seen wraparound produce the noise shown at high frequencies from a simple signal composed of 3 sine waves..  But the phase information gets crappy as you get close to Nyquist in addition to changing very rapidly.

The FFT of any  series, real or complex, is complex.  The phase information is contained in the relationship between the real and imaginary parts of the transform.  You determine the phase  by plotting the real and imaginary components in the XY plane with X as the real axis and Y as the imaginary axis.  In practice you calculate it using the arc tangent.  In complex signals unwrapping the phase correctly is quite challenging.

Export the time domain signal in .CSV format and read it in in Octave.  That will have the very real advantage of making you actually learn what the discrete Fourier series looks like.  You'll have to perform all the mathematical steps yourself.

If you don't acquire a skill this is all pointless and any piece of paper obtained will be worthless.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #38 on: March 10, 2018, 01:01:37 pm »
I just did a reconstruction in Excel, see pic below.  Something is off about the way the 'Frequency' column is calculated.
If I were to fiddle with the way the  'Frequency' column is calculated, or throw off the value of PI by a slight amount, I can make my reconstruction from the 3 points much better.  Almost perfect like the bottom 2 bumps.  What you see was calculated using only 3 cosines.  The formula is visible at the top...
__________
BrianHG.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #39 on: March 10, 2018, 01:14:34 pm »
Ok, if I add in the column A# * (1000/1024), it gets more accurate at the focal point.  Maybe if I added in another 6 sines, the adjacent bands each side of the 3 peaks, things might get more accurate.
« Last Edit: March 10, 2018, 01:23:22 pm by BrianHG »
__________
BrianHG.

#### amspire

• Super Contributor
• Posts: 3633
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #40 on: March 10, 2018, 04:16:33 pm »
I just had a quick go in Libre Calc to reconstruct the waveform.

The exercise said to use the principle harmonics, so I decided to use the first 1023 of them.

Got a pretty good graph, except that it was reversed in time - it starts at the end of the waveform and ends at the start! Not sure what I did there.

Other then that, is a pretty accurate match to the original.

I used time on the x axis rather then samples, and at each time interval, I just summed the cos and sin components for each harmonic.

Ended up with a 1024x1024 table with each cell having its own cos and sin sum. Each column corresponds to a time sample, so I just sum all the columns and plotted the results against time.
« Last Edit: March 10, 2018, 04:33:46 pm by amspire »

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #41 on: March 10, 2018, 04:53:54 pm »
Ohh yeah, those rotten little bastards...  The frequency coefficients are too perfect... Here is what I achieved using only 3 points from the FFT, not 1024:

Also:
The course paper says nothing about using an inverse Fourier.  It actually says reconstruct using the "principal harmonics" and even suggests sketching the waveforms which is clearly not going to exactly recreate the original.  Using the frequency, magnitude and phase of the three peaks (i.e. fundamental and two harmonics) gives a waveform similar to, but not exactly the same as the original as would be expected and is shown below.
Actually, after centering in on the 3 peak frequencies, fundamental + 2 harmonics, it's pretty much exact...  Take a look...
« Last Edit: March 10, 2018, 05:07:10 pm by BrianHG »
__________
BrianHG.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #42 on: March 10, 2018, 05:10:06 pm »
Note: in my Excel spreadsheet, you can change the three '0.9765625'  with  '(1000/1024)' (same value) in my sine generators.  This matches the frequency column 'J' where the readout is '=A1*18000/1024'  Note that also in my sine generators, I have 'PI()/9000', this is because the real way to write it should have been '2*PI()/18000'.  Once again I just did some shorthand.

Also, if you change the three '270' to any one new number, the waveform stays the same, it's just shifts the entire waveform to the left or right.  270 just happened to line up the waveforms perfectly.

As for the '*2' or '*-2' for the amplitude, when reading off of a spectrum, since the values have been made absolute positive only, there is a 50/50 chance that the sine wave amplitude is inverted.

The order of the multiply and divides could have been better though out before posting here, but, it works...
« Last Edit: March 10, 2018, 05:31:45 pm by BrianHG »
__________
BrianHG.

#### amspire

• Super Contributor
• Posts: 3633
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #43 on: March 10, 2018, 05:13:43 pm »
BrianHG, looks like you will get the gold star.

Obviously this is the aim of the exercise - you have essentially worked out the frequencies, amplitudes and phases they used to generate the original waveform.

This is an exercise that actually has a useful purpose!
« Last Edit: March 10, 2018, 05:16:07 pm by amspire »

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #44 on: March 10, 2018, 05:38:51 pm »
frequencies, amplitudes and phases they used to generate the original waveform.
Frequencies, just by looking at the points to the left and right of the center power, I made my best judgement call.
Amplitudes, I just used the signal level in the FFT.  It was good enough.  My reconstruction may have been better if I refined these numbers.
Phases, well, like I said, there is a 50/50 chance of it being positive or negative.
As for the relative offset of the entire waveform, that '270', I started with 0 and saw everything was just shifted off to the right, but the waveform pattern was perfect.  I then guessed 256, being a magic number, it was very close.  I then went up in increments of 5 and settled on 270.
__________
BrianHG.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #45 on: March 10, 2018, 06:15:05 pm »
BrianHG, looks like you will get the gold star.

Obviously this is the aim of the exercise - you have essentially worked out the frequencies, amplitudes and phases they used to generate the original waveform.

This is an exercise that actually has a useful purpose!

Arrrrggg, ok, this one is final.  I realized the 2 higher peaks in the waveform are exactly x5 and x7 from the fundamental.  This is why the pattern repeats forever without going all wacky.  The reconstruction only requires the base 51.2 Hz.  The other 2 waveforms are that frequency x5 and x7.  Now, my reconstruction, with adjusted amplitudes, is dead on!  This one is worth a gold star.  Take a look...
« Last Edit: March 10, 2018, 07:17:56 pm by BrianHG »
__________
BrianHG.

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #46 on: March 10, 2018, 07:27:59 pm »
Which is what I am attempting although the second and third peaks are not actually harmonics of the first as they are not an exact multiple. As pointed out earlier I need to establish the phase angle in order to get it right. if I don't I get the attached. I'll be delving into this tomorrow.

Yes they are exact multiples, the second peak is exactly x5 the first 51.2Hz.  And the third peak is exactly x7 the first 51.2Hz.  This is why the double spike pattern keeps repeating and my above post proves it giving dead on results removing all doubt.  These odd multiples have their cosines amplitudes inverted, or, you can call this 180 degrees out of phase.  Harmonics are usually odd number multiples.  This is also why we have 'third overtone crystals' and very old and rare 'fifth overtone crystals'.  These crystals have just 3x the number printed on their case.  IE a 75Mhz third overtone crystal is really just a 25MHz crystal.  You attach this crystal, or even a 25Mhz crystal to an approximately tuned 75Mhz LC oscillator, and, the 25MHz crystal will tame it and lock your LC oscillator's frequency at the 3x it's value, or 75MHz.

If you need an explanation of any part of how I worked this out, or any part of my attached Excel formula, let me know.
« Last Edit: March 10, 2018, 07:58:25 pm by BrianHG »
__________
BrianHG.

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #47 on: March 10, 2018, 07:58:59 pm »
so the peaks are not at the frequencies shown on the frequency column? the first peak is shown at 52.73........ Hz but you make it work with a different number.
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

#### BrianHG

• Super Contributor
• Posts: 2711
• Country:
##### Re: Reconstructing a waveform from its harmonics.
« Reply #48 on: March 10, 2018, 08:53:34 pm »
Ok, step 1, look at waveform and FFT to determine the accuracy of the peaks.

Finally seeing the waveform of 'Sample' from column B, the repeating pattern demands that the higher frequencies always repeat in the same area of the main 52hz indefinitely.

To check this, we starting with peak #1 and peak #2.
Peak #1 is strongest at 52.7hz, but, just a little slower as you see in my illustration, however, for now, we will call this 52.7hz.
Peak #2 is strongest at 246.1hz, but looking at the strength of the points to the left and right, we can see it should be a little higher.

Now for some math.  Take 246.1 and divide by 52.7 and you get approximately 4.7.  Now, we know this is too close to 5 to be a coincidence, so we do the reverse and take 52.7 x 5 = 263.5hz.  A little too high.

Lets also check peak 3 at 351.6hz / 52.7 = 6.7.  This is also to close to 7 and also lets see the opposite.  52.7*7=368.9hz, also a little too high.

Now if you are aware that the lower the frequency, the less accurate the Fourier hz measurement since it sees fewer sine oscillation over your 1024 samples compared to your higher frequency readings.  If you understand this, this next step will make sense.  If you don't, I'll show you another way to get a more accurate value on that 52.7hz...

Ok, refine the 3 frequencies, take them high ones and divide them down to 52.7hz.
peak3 = 351.6/7 = 50.2
peak2 = 246.1/5 = 49.2
peak1 = 52.7      = 52.7

take the average  50.2+49.2+52.7 = 152.1 / 3 = 50.7hz.  (I know this is still a little off, but, it will do for now)
This time if you redo the above 2 steps at the beginning, the number have gotten really close.

peak2 246.1hz/50.7hz=4.85.  (looking at the Fourier chart, we know point #2 should be a little higher than 246.1, this will even out)
peak3 = 351.6hz/50.7hz=6.93 (Again, looking  at the Fourier chart, we know point #3 should be only a pinch higher than 351.6, this will even out)

These 2 numbers peaks have gotten really close to 5x and 7x.  In fact now, if we multiply the base 50.7hz*5, we get 253.5hz and judging by the chart, this looks like it's the exact right value.  Same with 50.7hz*7 = 354.9hz, judging on the chart, this also looks perfect.

Next post, making a superior Excel spread sheet which allows you to play with these figures.  Give me 30 minutes...
« Last Edit: March 10, 2018, 08:57:15 pm by BrianHG »
__________
BrianHG.

#### Simon

• Global Moderator
• Posts: 12418
• Country:
• Did that just blow up? No? might work after all !!
##### Re: Reconstructing a waveform from its harmonics.
« Reply #49 on: March 10, 2018, 09:32:41 pm »
I see, if only my course taught me this stuff. Basically the frequency step is too large to resolve the exact frequency. I think that if this course was worth it's salt it would include lessons on the proper tools like matlab instead of trying to short cut it by hacking about with excel. I expect the fourier analysis was put into excel to keep the stock brokers hayy rather than the engineers.
https://www.simonselectronics.co.uk/shop New stock now in of EEVblog 121GW and Brymen 235 Now selling a selection of Probe Master probes.

Also, if you want to get ripped off: https://www.ebay.co.uk/usr/simons_electronics?_trksid=p2047675.l2559

Smf