DSPRelated.com
Forums

Accurate frequency detection

Started by ibra...@hotmail.com May 19, 2006
Hi everybody

I am using a dspic to detect the fundamental frequency of a music signal. I just want to know if it is easy to have a resolution less than +-0.5 Hz.

Thank you.

markanos



>Hi everybody I am using a dspic to detect the fundamental frequency of a music signal.
>I= just want to know if it is easy to have a resolution less than +-0.5 Hz.
>
>Thank you.
>
>markanos

Hi,

It depends on the signal. A common method is to use autocorrelation.
The autocorrelation can be computed with different "lag" time.
In other words by looking at a shorter or longer piece of your signal.
Looking at a larger extend in time leads to higher frequency resolution.
But if there are variations in the observed part of the signal,
your result might be useless. So if you have a stable periodic signal
(musical ?) you can get more accurate than +-0.5Hz. If you have quasiperiodic
signals and maybe vibrato or pitch jitter, naturally it is more problematic.

gr.
Anton
Hi Anton,

I want the detection to be in real time. I am afraid that autocorrelation method needs a lot of samples and a lot of computation time. No?
>Hi Anton,
>
>I want the detection to be in real time. I am afraid that autocorrelation method needs a lot of samples and a lot of computation time. No?

Hi,

It is absolutely possible in realtime, even on an older computer.
I never implemented one myself. But supercollider has an autocorrelation based
pitch detector, which works quite well, and since it is opensource you can
look at the implementation. Another tip is to look at the fiddle~
object by miller puckette. I think he also wrote a paper about the implementation.
This is not a simple autocorrelation based detector AFAIK, and it also works
in realtime.
A very simplistic approach is to build a detector based on zero crossings,
but that works only in a very limited amount of special cases. So the question
is: For what do you intend to use it?

gr.
Anton



I am using the Microchip DSP (dspic) to make an automatic piano tuner. So the signal comes from a piano string.

For the zero crossing method, i thought about it. It demands filtering the signal to eleminate harmonics and to avoid intermediate zero crossings. May be, one can electronically do filtering, than generate a square wave using comparator. The microprocessor will have only to detect the period of the square. (using capture module for example).

What do you say?

>
>> Hi Anton,
>>
>> I want the detection to be in real time. I am afraid that autocorrelation >method needs a lot of samples and a lot of computation time. No?
>
>Hi,
>
>It is absolutely possible in realtime, even on an older computer.
>I never implemented one myself. But supercollider has an autocorrelation b>ased
>pitch detector, which works quite well, and since it is opensource you can
>look at the implementation. Another tip is to look at the fiddle~
>object by miller puckette. I think he also wrote a paper about the impleme>ntation.
>This is not a simple autocorrelation based detector AFAIK, and it also work>s
>in realtime.
>A very simplistic approach is to build a detector based on zero crossings,>
>but that works only in a very limited amount of special cases. So the ques>tion
>is: For what do you intend to use it?
>
>gr.
>Anton
>
>
>
>
>I am using the Microchip DSP (dspic) to make an automatic piano tuner. So the signal comes from a piano string.

I really have no clue about how to build an automatic piano tuner.
Does that mean that you have a quite good approximation which
frequency you have to expect (because you know which string you
are tuning)? If so, it should be an advantage.

>
>For the zero crossing method, i thought about it. It demands filtering
>the signal to eleminate harmonics and to avoid intermediate zero
>crossings. May be, one can electronically do filtering, than generate
>a square wave using comparator. The microprocessor will have only to
>detect the period of the square. (using capture module for example).
>
>What do you say?

Your idea sounds sane to me (maybe that doesnt mean much ;).
But I have never met any really working acurate zero crossing based
pitch detector. I guess that the biggest problem with piano
strings is that you can have beatings and multiple nearby frequencies
in one tone. The harmonics of a piano are also a bit off. They
are not exact multiples. So filtering all harmonics away with
a steep filter just a bit over the expected frequency, might be
a good idea. Before you implement anything for the dspic, I guess
that you try stuff with matlab/octave or with testprograms in c.
Try some different methods and find out.
(I am interested to hear how it goes)

Also you might be interested in some papers (I think you can find
them with google. I don't know any urls)

stefan uppgard - Implementation and analysis of pitch tracking algorithms
david gerhard - Pitch Extraction and Fundamental Frequency: History and Current Techniques
P J Kootsookos - A review of frequency estimation and tracking problems

gr.
Anton



--- In a..., ibrahim_marc@... wrote:
>
> I am using the Microchip DSP (dspic) to make an automatic piano
tuner. So the signal comes from a piano string.
>
> For the zero crossing method, i thought about it. It demands
filtering the signal to eleminate harmonics and to avoid
intermediate zero crossings. May be, one can electronically do
filtering, than generate a square wave using comparator. The
microprocessor will have only to detect the period of the square.
(using capture module for example).
>
> What do you say?
>

If you look at the frequency spectrum of the string's signal, you'll
find not only harmonics. Especially in the beginning, there are
transient components of several frequencies, which are fading away
over time. They are responsible for the special "piano sound".
They will disturbe zero crossing analysis.
You'll have to wait until the signal has settled to the almost
harmonic mixture. Even then, you'll have some distortions (spurious
non-harmonic zero crossings).
You might want to remove these "wrong" zero crossings by some
statistical or probabilistic means.

If you try to improve your detection step by step, you'll probably
find yourself quite near the autocorrelation method, which is a
mathematical approach of what comes out of using the "fitting" zeros
and throwing away the "non-fitting".
So I guess that auto-correlation will help you to find a superior
solution to the zero-crossing detection.

Take these issues into account:
1) (Especially older) pianos are usually tuned below the normal
440Hz-a, to avoid the risk of a frame crash. (Or even for sake of a
better sound). So you cannot rely on pre-known frequencies.
2) A skilled piano tuner will end up with a brilliant piano sound,
but a frequency meter will show certain "mis-tuned" strings.
Ears are not hearing the same way as a frequency meter.
3) If you want to detect a tone of known frequency, you might use a
mixture of auto-correlation and cross-correlation between the
measured tone and a generated/calculated reference tone.
4) Generating square-waves before comparison might help to remove
most distortions. If you meditate this a while, you'll find out that
this will result in a sort of PLL.

Just my 2cts...
Bernhard
On Wednesday 31 May 2006 17:44, Jaime Andres Aranguren Cardona wrote:
> Hi,
>
> Maybe this simple mwethod helps: take the FFT of the
> input signal, then find the bin with the maximium
> value, which will correspond to the frequency of the
> measured signal. Of course, with this method, the
> accuracy depends completely on the length of the FFT.
>
> Bernhard, what do you say?
>
> JaaC

Hi JaaC,

that's the academic solution, which might or might not work here.
I'm not so familiar with the acoustic situation of a piano.
I could imagine, especially at the lower tones,
that the overtones could have more engery than the fundamental.
This would disturbe the analysis.
Then there are the resonances of the case (wood and steel), which will make
things even worse.

Then, as you indicated, it depends on the required resolution, if it can be
practically exploited.
If I have no guess, which frequency out of an allowed range
(for a piano tuner 20Hz...10000Hz should suffice) is correct,
the width of the bins must either be so narrow that the tones can be
distinguished. Let's say 1Hz.
Then you need at least 24kS/s to achieve the high end of the band.
Which results in 12000Hz/1Hz = 12000 bins. This is very long FFT.
Then the samples require a duration of a couple of seconds for a valuable
result. Even then, this result would be splendid for a high frequency tone.
It would not suffice for a very precise tuning.

An approach might combine a raw FFT to find a first guess for the tone.
This might work with less bins, and with a smaller duration (0.1sec?)
Then, you could take the result (find the bin with highest energy).
Use this result either to do another FFT with adapted resolution/sample rate,
or apply any other means (Goertzel?) to go on with.
Again, since it's the physical limitations, which we suffer from, it will be
the same as making a PLL lock on an unknown signal:
you need a broad capture region, and a very narrow lock region.
And you have to take care of everything between...which is always the
difficult part.

Might be worth to reflect the chance of having some preknowledge:
selecting the desired tone/frequency might make things easier.
And maybe even more comfortable for the user.

I used to work with a guitar tuner, which had three selectable modes:
- auto tuning
- preset tuning
- off
I started to use it in auto tuning mode. Which means, that it tries to find
out the tone which is played, and with two LEDs it signalled below/above.
Since the guitar has only 6 (or 4,10,12,...) strings, and because it was
annoying to watch the LCD display because it would switch the tone and it
happened, that I tuned to a wrong one, I dropped this method.
Then I started to use the preset tuning: I had to enter the tone manually,
let's say E. Then it would stay on this note, and I could evaluate only the
LEDs. Which worked quite well for me.
However, after some while, I dropped this method, too.
Since then, I mostly use the third mode (off). It's quicker, more precise and
works even with empty batteries.
If you play an instrument in combination with others (orchestra or band), it
doesn't help to know the exact frequency of a tone. It's more important that
it's the right tone with respect to the other tones and/or instruments.
A tuning fork (or the telephone's dial tone) with a 440Hz reference should
suffice - but that's philosophic polemy and doesn't help implementation.

Bernhard
Hi,

Maybe this simple mwethod helps: take the FFT of the
input signal, then find the bin with the maximium
value, which will correspond to the frequency of the
measured signal. Of course, with this method, the
accuracy depends completely on the length of the FFT.

Bernhard, what do you say?

JaaC

--- bholzmayer
wrote:

> --- In a..., ibrahim_marc@...
> wrote:
> >
> > I am using the Microchip DSP (dspic) to make an
> automatic piano
> tuner. So the signal comes from a piano string.
> >
> > For the zero crossing method, i thought about it.
> It demands
> filtering the signal to eleminate harmonics and to
> avoid
> intermediate zero crossings. May be, one can
> electronically do
> filtering, than generate a square wave using
> comparator. The
> microprocessor will have only to detect the period
> of the square.
> (using capture module for example).
> >
> > What do you say?
> >
>
> If you look at the frequency spectrum of the
> string's signal, you'll
> find not only harmonics. Especially in the
> beginning, there are
> transient components of several frequencies, which
> are fading away
> over time. They are responsible for the special
> "piano sound".
> They will disturbe zero crossing analysis.
> You'll have to wait until the signal has settled to
> the almost
> harmonic mixture. Even then, you'll have some
> distortions (spurious
> non-harmonic zero crossings).
> You might want to remove these "wrong" zero
> crossings by some
> statistical or probabilistic means.
>
> If you try to improve your detection step by step,
> you'll probably
> find yourself quite near the autocorrelation method,
> which is a
> mathematical approach of what comes out of using the
> "fitting" zeros
> and throwing away the "non-fitting".
> So I guess that auto-correlation will help you to
> find a superior
> solution to the zero-crossing detection.
>
> Take these issues into account:
> 1) (Especially older) pianos are usually tuned below
> the normal
> 440Hz-a, to avoid the risk of a frame crash. (Or
> even for sake of a
> better sound). So you cannot rely on pre-known
> frequencies.
> 2) A skilled piano tuner will end up with a
> brilliant piano sound,
> but a frequency meter will show certain "mis-tuned"
> strings.
> Ears are not hearing the same way as a frequency
> meter.
> 3) If you want to detect a tone of known frequency,
> you might use a
> mixture of auto-correlation and cross-correlation
> between the
> measured tone and a generated/calculated reference
> tone.
> 4) Generating square-waves before comparison might
> help to remove
> most distortions. If you meditate this a while,
> you'll find out that
> this will result in a sort of PLL.
>
> Just my 2cts...
> Bernhard
>

Jaime Andr Aranguren Cardona
j...@ieee.org
j...@computer.org
__________________________________________________
Hi,

What you said Bernhard is in fact exact, concerning the fft and the low frequencies. The human tuners do not in general tune the low pitches individually. They tune them according the the higer octave etc...

For my automatic tuner i am working on, the spped is important. The user must hit the note once, and the tuner must do its job. So, the pitch detection must be accurately finished in 3 or 4 seconds.

After investigating piano signals on matlab, i think that correlation methods will be the most suitable. May be a small fft will be useful for detecting approximately the frequency at the begining.

I have done some experiences concerning fft. I had the following ideas connclusions.

1- Zero padding :
-----------------
Since we want to detect the fundumental frequency, we do not need to have great resolution(in term of seperating two different frequencies). All we need is a high precision for the value of the fundamental frequency. Here, i remarked that zero padding is of great interest. In fact, with only 32 samples, or even 16, then zero padding till having Fs/N = precision wanted, we can measure the frequency. I know that, the fft calculation time will the same since real samples are replaced with zero, but may it will be useful for decreazing the sampling duration

2- fft variant
-----------------
I will explain my idea with an example. We take 16 samples of the signal, we make the fft X(k). let k0 be the greater frequency (X(k0) is a maximum). We know at this level that the fundamental frequency is within k0-1 and k0+1. What i noticed is that we can directly refine this interval: in fact, if X(k0+1) > X(k0-1), then fundamental frequency will be between k0 and the k0+1, if not, it is in [k0-1, k]. We will call the bound k1 (k1=k0+ or k0-1).
Now, we compute the DFT coefficient for only the intermediate frequency k2=(k0+k1)/2 (that means as DFT of 32 points, where the last 16 samples are zeros). we obtain a value X(k2)> X(k1). (this is due to the fact that thers is no other frequencies in this range). now the frequency is in [k0, k2]. we can reiterate this procedure n times until the wanted precision.

The result is the same as if we calculated the fft of 16*2^n points. The only difference is that we calculate only the just neede coefficients. May be this is Goertzel idea? Is it quicker than doing the correspondent fft?

What do you think about it? I noticed that experimentally. I have no proofs.

Thank you for all your ideas.

m.