Breaking down a waveform into harmonics

joshb
 KVRist
 95 posts since 13 Apr, 2016
Breaking down a waveform into harmonics
Sorry if this is lame, but I'd like to calculate how much of each harmonic is in a waveform. I imagine this involves using FFT, but I have very little experience with them. I've always seen FFTs as rather broad ranges and what I'm looking for is something more like what percentage of each harmonic/frequency is in the waveform.
Can someone point me out to some sample code along those lines?
Thanks.
Can someone point me out to some sample code along those lines?
Thanks.

Miles1981
 KVRian
 1374 posts since 26 Apr, 2004 from UK
Re: Breaking down a waveform into harmonics
FFT, then compute the amplitude (abs value of the complex number) and you have the amplitude for each frequency. The precision depends on the number of samples you are using.

AnalogGuy1
 KVRian
 555 posts since 6 Mar, 2005 from USA
Re: Breaking down a waveform into harmonics
And just to give some stepbystep instructions, say you start out with N samples of signal x[n] sampled at fs and you want to find out what percentage of its energy is at frequency f0.
1) take the FFT of x[n] to get X[k]
2) find the index k0 corresponding to f0 by k0 = round(N*f0/fs)
3) the energy E0 at f0 in the signal is (X[k0])^2
4) find the total energy of the signal E = sum(x[n]^2)
5) so the percentage of the signal energy in the frequency bin that contains f0 is E/E0.
The devil's in the details though;
* your frequency bin width is inversely proportional to N
* this is the whole signal frequency content, and you probably want to first pick out subsections of it
* without first sending the x[n] through a Hamming window you will pick up strong images of the sinc wiggle which is the freqdomain transform of the rectangle you've effectively windowed x[n] by.
1) take the FFT of x[n] to get X[k]
2) find the index k0 corresponding to f0 by k0 = round(N*f0/fs)
3) the energy E0 at f0 in the signal is (X[k0])^2
4) find the total energy of the signal E = sum(x[n]^2)
5) so the percentage of the signal energy in the frequency bin that contains f0 is E/E0.
The devil's in the details though;
* your frequency bin width is inversely proportional to N
* this is the whole signal frequency content, and you probably want to first pick out subsections of it
* without first sending the x[n] through a Hamming window you will pick up strong images of the sinc wiggle which is the freqdomain transform of the rectangle you've effectively windowed x[n] by.

AnalogGuy1
 KVRian
 555 posts since 6 Mar, 2005 from USA
Re: Breaking down a waveform into harmonics
In step 3 I should have said X[k0] * X*[k0], where the first asterix means multiplication and the second means complex conjugation.

mystran
 KVRAF
 5244 posts since 12 Feb, 2006 from Helsinki, Finland
Re: Breaking down a waveform into harmonics
If you have a nice "digital" singlecycle waveform of some integer (preferably power of two) length, then just FFT the thing "as is" and you got harmonics directly in the FFT bins (ie. amplitude is the complex magnitude, phase is the complex angle).joshb wrote:Sorry if this is lame, but I'd like to calculate how much of each harmonic is in a waveform. I imagine this involves using FFT, but I have very little experience with them. I've always seen FFTs as rather broad ranges and what I'm looking for is something more like what percentage of each harmonic/frequency is in the waveform.
If you have something that isn't quite perfect singlecycle then you'll have to estimate the harmonics one way or another by looking at the FFT peaks and trying to guess what corresponds to what harmonic, but ... other than that the same basic idea applies.
If you'd like Signaldust to return, please ask Katinka Tuisku to resign.

joshb
 KVRist
 95 posts since 13 Apr, 2016
Re: Breaking down a waveform into harmonics
I'm just coming back to this after a while and I've got a few more specific questions and use cases.
I want to model/synthesize some tune percussion instruments (tubular bells, chimes, vibes, etc) and I thought I'd do that via modal synthesis or maybe additive. What it seems to come down to is:
 the number of harmonics
 their frequencies
 their initial relative volumes
 their decay rates
When I look at a sample playing back through an FFT, I can see the number of harmonics and their frequencies fairly easily, but it looks like the FFT reacts pretty slowly to such a percussive signal and I have to wonder about the temporal accuracy.
I wonder what the best way to analyze this would be. I thought about coding something that would feed several small slices of the waveform through an FFT and look at the bin levels to calculate relative volumes over time. But...what size should each sample be? The size of the frequency of the lowest harmonic? More than that? How accurate would that be?
And if FFT is too slow for such a percussive signal, what about measuring the output of a band pass filter at every harmonic frequency? Would that have better time accuracy? But at what cost?
I imagine that there's tradeoffs no matter what. Any thoughts on these methodologies? Or better ideas? Thanks.
I want to model/synthesize some tune percussion instruments (tubular bells, chimes, vibes, etc) and I thought I'd do that via modal synthesis or maybe additive. What it seems to come down to is:
 the number of harmonics
 their frequencies
 their initial relative volumes
 their decay rates
When I look at a sample playing back through an FFT, I can see the number of harmonics and their frequencies fairly easily, but it looks like the FFT reacts pretty slowly to such a percussive signal and I have to wonder about the temporal accuracy.
I wonder what the best way to analyze this would be. I thought about coding something that would feed several small slices of the waveform through an FFT and look at the bin levels to calculate relative volumes over time. But...what size should each sample be? The size of the frequency of the lowest harmonic? More than that? How accurate would that be?
And if FFT is too slow for such a percussive signal, what about measuring the output of a band pass filter at every harmonic frequency? Would that have better time accuracy? But at what cost?
I imagine that there's tradeoffs no matter what. Any thoughts on these methodologies? Or better ideas? Thanks.

mystran
 KVRAF
 5244 posts since 12 Feb, 2006 from Helsinki, Finland
Re: Breaking down a waveform into harmonics
There is a fundamental tradeoff between temporal and spectral accuracy. It applies to FFT, but it also applies to bandpass filter banks: as you make the filters more selective, they will also react more slowly.
So I'm afraid there isn't any magic bullet.
I suppose if you can get estimates of the modal frequencies, you could then try to fit a modal resonator bank to the impulse response using least squares or something, to try to match the decays without actually having to analyse them first. This is really just a guess though, it's not something I've actually ever tried.
If you'd like Signaldust to return, please ask Katinka Tuisku to resign.

DaveClark
 KVRist
 158 posts since 8 May, 2007
Re: Breaking down a waveform into harmonics
Hi joshb,
One typical technique for analysis is to look at spectrograms:
https://en.wikipedia.org/wiki/Spectrogram
In this way, you can see where the signal starts and how it decays for various frequencies.
Regards,
Dave Clark
One typical technique for analysis is to look at spectrograms:
https://en.wikipedia.org/wiki/Spectrogram
In this way, you can see where the signal starts and how it decays for various frequencies.
Regards,
Dave Clark

joshb
 KVRist
 95 posts since 13 Apr, 2016
Re: Breaking down a waveform into harmonics
Hey Dave,
But looking at a spectrogram doesn't seem very measurable or accurate...unless I'm missing something. I was hoping to write some code that could give me the level and decay of each harmonic somewhat accurately. FFT seems mostly accurate for pitch, but lags in timing. Using bandpass filters seems that to get pitch accuracy, I'd lose temporal accuracy.
But looking at a spectrogram doesn't seem very measurable or accurate...unless I'm missing something. I was hoping to write some code that could give me the level and decay of each harmonic somewhat accurately. FFT seems mostly accurate for pitch, but lags in timing. Using bandpass filters seems that to get pitch accuracy, I'd lose temporal accuracy.

mystran
 KVRAF
 5244 posts since 12 Feb, 2006 from Helsinki, Finland
Re: Breaking down a waveform into harmonics
Use two measurements, one that's accurate in pitch (to figure out where the modes are) and then another one that's more accurate in time (to estimate how fast they decay)?
If you'd like Signaldust to return, please ask Katinka Tuisku to resign.

joshb
 KVRist
 95 posts since 13 Apr, 2016
Re: Breaking down a waveform into harmonics
I can find the pitches reasonably well, it's the levels of each harmonic at their peak that I'm most interested in.
I guess I could find a single cycle of the waveform immediately after the strike and just FFT analyze that one cycle. Maybe that's the best way to get their peak levels. Would that be accurate?
Then, maybe, use a normal FFT of the entire sample for the decay times...thinking that the lag would have somewhat settled down after the strike.
Maybe?
I guess I could find a single cycle of the waveform immediately after the strike and just FFT analyze that one cycle. Maybe that's the best way to get their peak levels. Would that be accurate?
Then, maybe, use a normal FFT of the entire sample for the decay times...thinking that the lag would have somewhat settled down after the strike.
Maybe?

aciddose
 KVRAF
 12164 posts since 7 Dec, 2004
Re: Breaking down a waveform into harmonics
One oft overlooked reality is the DFT (discrete fourier transform) is based upon the FT (fourier transform) which is a way to break a continuous signal down into harmonic components.
The FFT is simply a way to break the DFT down into smaller optimized chunks by observing that you can compute an 8 sample chunk by computing two 4 sample chunks with some shuffling; so FFT is essentially a factorization of the discrete transform.
With that said you can start to understand your real focus should be on the FT itself and the implications of the DFT: discretization meaning there are a fixed number of "bins" and they represent harmonic partials exactly in a 100% reversible transformation related to the nyquistshannon sampling theorem: for 128 samples there are 64 complex partials and an offset "d" which should be familiar to those who understand basic calculus which is the basis for Fourier's equations.
If you start from the beginning you can get a clearer idea of why things work as opposed to just how. That can help a lot with understanding why "measure the harmonics in a transient signal" is impossible and what the tradeoffs are in approximating it.
The FFT is simply a way to break the DFT down into smaller optimized chunks by observing that you can compute an 8 sample chunk by computing two 4 sample chunks with some shuffling; so FFT is essentially a factorization of the discrete transform.
With that said you can start to understand your real focus should be on the FT itself and the implications of the DFT: discretization meaning there are a fixed number of "bins" and they represent harmonic partials exactly in a 100% reversible transformation related to the nyquistshannon sampling theorem: for 128 samples there are 64 complex partials and an offset "d" which should be familiar to those who understand basic calculus which is the basis for Fourier's equations.
If you start from the beginning you can get a clearer idea of why things work as opposed to just how. That can help a lot with understanding why "measure the harmonics in a transient signal" is impossible and what the tradeoffs are in approximating it.
Free plugins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.

mystran
 KVRAF
 5244 posts since 12 Feb, 2006 from Helsinki, Finland
Re: Breaking down a waveform into harmonics
Strictly speaking, the reason one has fixed bandwidth bins is an artifacts of the periodic transform. When you take such a periodic transform in continuous time, you get the Fourier Series. The (time)discretization just means there is finite number of such bins (or depending on your viewpoint, the same bins loop once you do a roundtrip around the zplane unit circle).aciddose wrote: ↑Sat Mar 23, 2019 9:54 amWith that said you can start to understand your real focus should be on the FT itself and the implications of the DFT: discretization meaning there are a fixed number of "bins" and they represent harmonic partials exactly in a 100% reversible transformation related to the nyquistshannon sampling theorem: for 128 samples there are 64 complex partials and an offset "d" which should be familiar to those who understand basic calculus which is the basis for Fourier's equations.
You CAN theoretically also compute (nonperiodic) "discretetime fourier transform" but there's not much point, since you would have to take this over an infinite signal and if you window a finite segment (and treat rest of the samples as zero), all you get is a sincinterpolated DFT spectrum which you could also obtain (to your desired finite resolution) by zeropadding a regular (periodic) FFT until you have as much "resolution" as you want. Still, this doesn't increase the information content in any way, you just get interpolated version of the same thing.
If you'd like Signaldust to return, please ask Katinka Tuisku to resign.

earlevel
 KVRist
 483 posts since 4 Apr, 2010
Re: Breaking down a waveform into harmonics
By chance, the next session of this free course starts tomorrow. I completed the inaugural session a couple of years ago—it's very good. Basically, you're looking for the harmonic model. You're introduced to a python toolkit (smstools) that makes it easy to put together your own code, and existing tools to analyze and plot the harmonics over time.
https://online.stanford.edu/courses/soh ... plications
https://online.stanford.edu/courses/soh ... plications
My audio DSP blog: earlevel.com

DaveClark
 KVRist
 158 posts since 8 May, 2007
Re: Breaking down a waveform into harmonics
Hi joshb,joshb wrote: ↑Sat Mar 23, 2019 8:57 amHey Dave,
But looking at a spectrogram doesn't seem very measurable or accurate...unless I'm missing something. I was hoping to write some code that could give me the level and decay of each harmonic somewhat accurately. FFT seems mostly accurate for pitch, but lags in timing. Using bandpass filters seems that to get pitch accuracy, I'd lose temporal accuracy.
A spectrogram can be as accurate as your sampling rate and length of time for measurement. It's a time series of FFTs, so there is nothing about the accuracy of a spectrogram that is different from that of an FFT.
You mentioned some percussive types of tone instruments. For rapid decays, you need as high a sampling rate as you can possibly obtain. Measure long enough for a full decay. You can add pads at the end and beginning for more resolution. Take an FFT for every sample: FFT, move over one sample, FFT, .... As aciddose mentioned, there are fundamental (and technological) limits on how well you can do this.
You mentioned a lag. That's the time it takes to accumulate all of the samples for FFT. It's not something that affects the analysis except that you cannot easily see what it's doing in real time.
Regards,
Dave Clark
Somewhat of a diversion, but if you have time and/or are unfamiliar with certain aspects:
The concept of frequency "bins" is highly misleading. This suggests that there are containers that hold the true frequencies of a signal inside of them. Nothing could be further from the truth. A signal is decomposed into a handful of specific component frequencies. Any signal whose frequency (frequencies) does not exactly match those specific frequencies for the entire block will be forced into producing results that give nonzero values (e.g. magnitude and phase) for more than one frequency. As you move along the signal, you will get different values (e.g. magnitudes and phases) for those component frequencies.
This has implications for the small "time slices" that you had proposed taking. Although the analysis of each particular time slice would accurately represent an infinite periodic wave that had that time slice over and over again, the result as well as statistical results such as the average would not accurately represent the fundamental or any harmonic component.
It also has implications for longer "time slices." The values for the component frequencies will reflect the magnitude and phase over a long period of time, like an average. If you have, say, some vibrato, then you will likely have a number of frequencies that show up with relatively similar values; the variations will be lost.
Understanding the physics behind a physical instrument is crucial to making sense out of measurements because of these limitations, much like the situation in modern physics (the Uncertainty Principle). The time lag that you are worried about should be among the least of your concerns. You're stuck with trying to obtain high resolution in the frequency OR the time domain for any particular analysis. Trying to combine analyses may provide some insight but generally will not help much because the time behavior of one particular analysis isn't going to reflect that of another. Knowing what is physically happening is a big help. This would allow you to make choices for optimal resolution in both domains simultaneously. You will be able to say, for example: Well, the frequency isn't that clear, but I know it has to be X Hz, and I can see how it builds then falls away.