DSPRelated.com
Forums

Algorithm for musical instrument tuner

Started by Paul Howland November 16, 2004
Hello.  As my first programming project using Objective-C under MacOS X, I'd 
like to write a simple musical instrument tuner that takes samples from the 
microphone, measures the instrument's frequency and displays a graphical 
meter that indicates whether the note is too flat or too sharp.  What is the 
most effective algorithm for estimating the instrument's frequency?  I 
assume the FFT is an overkill.  Is there some trick with zero-crossings that 
can be used?  Of course, in reality I am only interested in, say, 6 notes 
(for a guitar) or 4 for a violin - and then a means of knowing which is the 
closest note being played and whether it is higher or lower in pitch.

Any ideas?

Thanks,

Paul 


Paul Howland wrote:

> Hello. As my first programming project using Objective-C under MacOS X, I'd > like to write a simple musical instrument tuner that takes samples from the > microphone, measures the instrument's frequency and displays a graphical > meter that indicates whether the note is too flat or too sharp. What is the > most effective algorithm for estimating the instrument's frequency? I > assume the FFT is an overkill. Is there some trick with zero-crossings that > can be used? Of course, in reality I am only interested in, say, 6 notes > (for a guitar) or 4 for a violin - and then a means of knowing which is the > closest note being played and whether it is higher or lower in pitch. > > Any ideas? > > Thanks, > > Paul > >
A short web search yielded http://www.tedknowlton.com/resume/CCPPT.htm. I'd like to know how to do this, too, but mostly because our Korg tuner comes in a boring square case, whereas I want one that's shaped like a fish (you can guess the stupid pun...) -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Paul,

doing a reasonably sized FFT on a G4/G5 Mac isn't overkill, but it's 
not really the ideal tool to do this from the DSP viewpoint.

Many instruments (and, most importantly, human voice) produce sounds 
where picking the frequency with the highest amplitude will give you 
the 2nd or 3rd harmonic and not the fundamental frequency.

Therefore, you could be better off using the short term autocorrelation 
or average magnitude difference for this purpose, both of which will be 
stable even with sounds that have their fundamental frequency missing.

As a reference, Rabiner and Gold ("Digital Processing of Speech 
Signals") discuss various methods for pitch detection including 
autocorrelation and AMDF which you could explore for your purpose.
-- 
Stephan M. Bernsee
http://www.dspdimension.com

Stephan M. Bernsee wrote:

> Paul, > > doing a reasonably sized FFT on a G4/G5 Mac isn't overkill, but it's not > really the ideal tool to do this from the DSP viewpoint. > > Many instruments (and, most importantly, human voice) produce sounds > where picking the frequency with the highest amplitude will give you the > 2nd or 3rd harmonic and not the fundamental frequency. > > Therefore, you could be better off using the short term autocorrelation > or average magnitude difference for this purpose, both of which will be > stable even with sounds that have their fundamental frequency missing. > > As a reference, Rabiner and Gold ("Digital Processing of Speech > Signals") discuss various methods for pitch detection including > autocorrelation and AMDF which you could explore for your purpose.
Are you talking about doing the autocorrelation in the time domain, and looking for peaks at the fundamental? Certainly you don't mean autocorrelate and take the FFT -- that would be the same thing as taking the FFT and doing a mag-squared operation. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Tim Wescott wrote:

> Paul Howland wrote: > >> Hello. As my first programming project using Objective-C under MacOS >> X, I'd like to write a simple musical instrument tuner that takes >> samples from the microphone, measures the instrument's frequency and >> displays a graphical meter that indicates whether the note is too flat >> or too sharp. What is the most effective algorithm for estimating the >> instrument's frequency? I assume the FFT is an overkill. Is there >> some trick with zero-crossings that can be used? Of course, in >> reality I am only interested in, say, 6 notes (for a guitar) or 4 for >> a violin - and then a means of knowing which is the closest note being >> played and whether it is higher or lower in pitch. >> >> Any ideas? >> >> Thanks, >> >> Paul >> > A short web search yielded http://www.tedknowlton.com/resume/CCPPT.htm. > > I'd like to know how to do this, too, but mostly because our Korg tuner > comes in a boring square case, whereas I want one that's shaped like a > fish (you can guess the stupid pun...)
I'm too ignorant to appreciate the pun. Why determine the fundamental by interpolating a FT? does the changing harmonic content during the decay introduce errors into a period measurement? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Jerry Avins wrote:

> Tim Wescott wrote: >>A short web search yielded http://www.tedknowlton.com/resume/CCPPT.htm. >> >>I'd like to know how to do this, too, but mostly because our Korg tuner >>comes in a boring square case, whereas I want one that's shaped like a >>fish (you can guess the stupid pun...) > > > I'm too ignorant to appreciate the pun. Why determine the fundamental by > interpolating a FT? does the changing harmonic content during the decay > introduce errors into a period measurement? > > Jerry
You're probably too sophisticated to catch it -- say "tuner", but like any good southie resident take the 'r' off to use when you have a good idea. Many instruments have a sound with a severely supressed fundamental, so even discounting noise just counting zero crossings won't get you there. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On 2004-11-16 22:10:36 +0100, Tim Wescott <tim@wescottnospamdesign.com> said:

> Are you talking about doing the autocorrelation in the time domain, and > looking for peaks at the fundamental?
Yes. Or for "valleys" in the AMDF.
> Certainly you don't mean autocorrelate and take the FFT -- that would > be the same thing as taking the FFT and doing a mag-squared operation.
No pun intended but I'm not sure how you come to conclude this from my posting - I didn't mention the FFT in connection with the autocorrelation or AMDF, did I? You can calculate the autocorrelation using the FFT (which might be quicker if you don't have a fast MAC instruction), but you don't have to... -- Stephan M. Bernsee http://www.dspdimension.com
Stephan M. Bernsee wrote:

> On 2004-11-16 22:10:36 +0100, Tim Wescott <tim@wescottnospamdesign.com> > said: > >> Are you talking about doing the autocorrelation in the time domain, >> and looking for peaks at the fundamental? > > > Yes. Or for "valleys" in the AMDF. > >> Certainly you don't mean autocorrelate and take the FFT -- that would >> be the same thing as taking the FFT and doing a mag-squared operation. > > > No pun intended but I'm not sure how you come to conclude this from my > posting - I didn't mention the FFT in connection with the > autocorrelation or AMDF, did I? > > You can calculate the autocorrelation using the FFT (which might be > quicker if you don't have a fast MAC instruction), but you don't have to...
I was just making sure. As for the FFT only being faster if you don't have a MAC, that would be an interesting thing to benchmark, since there are many DSP chips that have FFT-acceleration hardware to go with their MAC units -- and a MAC should speed up an FFT. Do you know if it's been done? Are you speaking from experience? Is the autocorrelation generally faster any time you have a MAC, or does it vary from processor to processor? -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
"Paul Howland" <howland@wanadoo.nl> wrote in message news:<419a4fa9$0$30710$18b6e80@news.wanadoo.nl>...
> Hello. As my first programming project using Objective-C under MacOS X, I'd > like to write a simple musical instrument tuner that takes samples from the > microphone, measures the instrument's frequency and displays a graphical > meter that indicates whether the note is too flat or too sharp. What is the > most effective algorithm for estimating the instrument's frequency? I > assume the FFT is an overkill. Is there some trick with zero-crossings that > can be used? Of course, in reality I am only interested in, say, 6 notes > (for a guitar) or 4 for a violin - and then a means of knowing which is the > closest note being played and whether it is higher or lower in pitch. > > Any ideas? > > Thanks, > > Paul
You could always try to put aliasing to good use. That is, sample at the frequency you are tuning to and then the sampled signal will have spectral lines corresponding to how far from true the note is. The only catch is it could be too low or it could be too high and you won't know which unless you sample at a slightly higher rate as well. Then if the fundamental in the higher sampling rate case is higher than in the first case you've moved away from correct tuning so you need to increase the frequency of the note. You might wonder why I'm suggesting aliasing. The reason is if you put it to good use the tuning resolution you can get is much higher than sampling without aliasing and using FFT's. Basically you can get much more detail with a much lower sampling rate but it all hinges on the signal being strictly periodic. with inharmonicity there may be some complications, but it would make for an interesting experiment. I'd thought of trying this out in the past but haven't had the opportunity. Regards, Paavo Jumppanen Author of Har-Bal Harmonic Balancer http://www.har-bal.com
On 2004-11-17 07:19:51 +0100, Tim Wescott <tim@wescottnospamdesign.com> said:

> As for the FFT only being faster if you don't have a MAC, that would be > an interesting thing to benchmark, since there are many DSP chips that > have FFT-acceleration hardware to go with their MAC units -- and a MAC > should speed up an FFT. Do you know if it's been done? Are you > speaking from experience? Is the autocorrelation generally faster any > time you have a MAC, or does it vary from processor to processor?
Yes you're right - I was thinking about comparing autocorrelation and AMDF, not autocorrelation with and without FFT. In general, for sizes in the 1024-4096 pt range the autocorrelation using an FFT is faster on a general purpose CPU than the direct calculation. I've not coded this in assembly yet so there might be a way to speed up the autocorrelation further (by using vector unit instructions for example). But that goes for the FFT as well. Comparing autocorrelation with AMDF (which was what I was actually thinking) - the latter is usually faster on a general purpose CPU, but things can be different if you have a fast MAC instruction available. -- Stephan M. Bernsee http://www.dspdimension.com