DIYstompboxes.com

DIY Stompboxes => Digital & DSP => Topic started by: potul on August 05, 2011, 05:21:48 PM

Title: Pitch detection
Post by: potul on August 05, 2011, 05:21:48 PM
Hi,

I've been doing some research around pitch detection algorithms, that I plan to use in  a couple of projects, including a Guitar-2-midi converter using a dsPIC.

I have compared Autocorrelation, STFT and HPS, and done some testing in MPLAB. What I've found is that for a given sampling rate and block size:

-Autocorrelation works best on low frequencies
-STFT works best on high frequencies
-HPS is great to distinguish the fundamental frequency from the harmonics.

Which is not surprising, because:

-Autocorrelation resolution depends on the sampling rate. The closest frequencies you can distinguish are separated by a period of one sample, so for low frequencies (long periods), the error is smaller.
-FFT resolution is constant in hz, but frequency differences between notes are much higher for high frequencies, so the errors are smaller.


So, I decided to put a hybrid algorithm, something like this:

Step1: Perform a first  FFT
Step2: Do an HPS and calculate the fundamental frequency. At this point resolution is very bad, and determined  by the FFT bins width
Step3a: If the fundamental frequency is low, perform and Autocorrelation. This will give us the frequency with a much higher resolution
Step3b: If the fundamental frequency is high, perform another FFT with an overlap to the first one (it would be the next frame in the STFT). With this, calculate the phase difference for the fundamental bin and apply the necessary correction to compute the real frequency. This also gets us to a much better resolution than the FFT bins.

I've implemented the code and tested it in MPLAB (didn't have time to test in a real PIC yet), using real guitar samples, and the results are promising. But I have a doubt regarding the latency.

The autocorrelation takes around 4 ms to compute and the FFTs around 1.5 ms. This looks acceptable, but what concerns me is the size of the block needed.  I would assume that in order to be able to track the lowest frequencies I need to analyse at least a block corresponding to the period of the lowest note. This is around 12ms for the low E. But for the autocorrelation to work I think I need more, as I need to detect repetitions of the signal.
By doing some experimentation I've found the optimal to be around 25 ms.
So, this would mean a latency of around 30ms just for the sampling and autocorrelation, and probably I would need to add some more computation. But I see that the total latency would be around 50ms or so.

Would a latency of around 50ms be too much for a guitar-2MIDI device?
Do you have any experience with autocorrelation?

Mat
Title: Re: Pitch detection
Post by: Hides-His-Eyes on August 05, 2011, 06:01:52 PM
50ms is too much latency for recording. But a synth sound might easily have an attack of 100ms. So it really depends what it's being used for.
Title: Re: Pitch detection
Post by: Taylor on August 06, 2011, 01:19:19 AM
A programmed pitch-detection PIC would be a very popular DIY product IMO! I'd buy some for sure, and once a project or several arose using it, it would be a hot item. (Sorry I have no useful advice for you).
Title: Re: Pitch detection
Post by: potul on August 06, 2011, 06:29:51 AM
Ok, I'll give it a try with the 50ms latency and see how it feels. Now I'm 3 weeks out on a trip an can only work in the simulator, but once I get back home I'll try to burn it and see how it feels.

Taylor, regarding the pitch-detection chip, I wonder what applications could have... Once I finish my guitar-2-midi project, it should not be complicated to remove the midi functionality and add something to make it work more generically.
I wonder how it would be convenient to interface other devices, outputting a CV? Or maybe generating a clock signal? What do you envision?
Title: Re: Pitch detection
Post by: cpm on August 06, 2011, 07:49:18 AM
what about parallelizing the computations, so when the first FFT is done you already have the previosus results from the different low and high methods, even if they are from previous samples.
If enough memory and processor is available you could even do overlapped calculations. Instead of doing 1 20ms correlation algorithm at a time, perform 4 (still 20ms each one) at each 5ms tap, and apply some heuristics over the results to better aproximate, and get less latency and less glitches.

does it make any sense?

regarding a general purpose module: both a CV (pwm) and a square output locked to the detected frequency would be excellent
Title: Re: Pitch detection
Post by: CynicalMan on August 06, 2011, 08:24:49 AM
I hate to be a downer, but Distortion with lots of low-pass filtering generally do pretty well at monophonic pitch detection. Play a Big Muff with Sustain set high. That gets the fundamental as a square wave pretty easily, and it doesn't get fooled by harmonics. Now polyphonic pitch detection would be sweet. Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.
Title: Re: Pitch detection
Post by: Taylor on August 06, 2011, 03:13:34 PM
Quote from: CynicalMan on August 06, 2011, 08:24:49 AM
I hate to be a downer, but Distortion with lots of low-pass filtering generally do pretty well at monophonic pitch detection. Play a Big Muff with Sustain set high. That gets the fundamental as a square wave pretty easily, and it doesn't get fooled by harmonics. Now polyphonic pitch detection would be sweet. Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.

Something along those lines can indeed be good signal conditioning before pitch detection (although filtering and squaring can be done much more easily in software than having to build a Big Muff), but how does that equal pitch detection? All the detection would still need to be done after that point to get a CV, varying resistance, or any controller based on the pitch of the signal.

Quote from: potul on August 06, 2011, 06:29:51 AM
Taylor, regarding the pitch-detection chip, I wonder what applications could have... Once I finish my guitar-2-midi project, it should not be complicated to remove the midi functionality and add something to make it work more generically.
I wonder how it would be convenient to interface other devices, outputting a CV? Or maybe generating a clock signal? What do you envision?

Outputting a CV would indeed be magical. One could take that and then do whatever else with it fairly easily. If there was a way to get a precise varying resistance, that would be cool too, as the monolithic function generator ICs like the XR2206 need a resistance for pitch control as I recall. I suppose a digipot would be one solution if there was integrated feedback from the digipot so the PIC could make up for tolerance issues in the digipots. There would be that zippering effect due to limited digipot resolution, but I think this could be smoothed, or looked at as a feature, like a built-in microtonal Auto-Tune.  :D

Here are some apps off the top of my head, for which I think people would be excited to use this:

-guitar/bass synths (if a simple PIC circuit interfaced with the XR2206 you could have a very tiny, and very capable synth)
-pitch tracking filters
-pitch tracking ring modulators
Title: Re: Pitch detection
Post by: slacker on August 06, 2011, 03:55:07 PM
Taylor, the XR2206 can be voltage controlled, there's a design by Thoms Henry here http://www.birthofasynth.com/Thomas_Henry/Pages/XR-VCO.html (http://www.birthofasynth.com/Thomas_Henry/Pages/XR-VCO.html) needs a bipolar supply but other than that it looks a fairly straightforward build.

A pitch to CV chip would be great, I'd imagine once you've done the pitch to midi part the CV would be fairly trivial. Look forward to seeing how this progresses.   
Title: Re: Pitch detection
Post by: CynicalMan on August 06, 2011, 10:17:43 PM
Quote from: Taylor on August 06, 2011, 03:13:34 PM
Quote from: CynicalMan on August 06, 2011, 08:24:49 AM
I hate to be a downer, but Distortion with lots of low-pass filtering generally do pretty well at monophonic pitch detection. Play a Big Muff with Sustain set high. That gets the fundamental as a square wave pretty easily, and it doesn't get fooled by harmonics. Now polyphonic pitch detection would be sweet. Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.

Something along those lines can indeed be good signal conditioning before pitch detection (although filtering and squaring can be done much more easily in software than having to build a Big Muff), but how does that equal pitch detection? All the detection would still need to be done after that point to get a CV, varying resistance, or any controller based on the pitch of the signal.


My point is that using FFTs isn't necessary for monophonic pitch detection. Clipping and LPFs will get you a square wave, then you can just count the pluses for the pitch. Going from there to a CV is pretty easy. FFTs are nice for polyphonic pitch detection, but then we're looking at a few CV outputs instead of one pin. Not that I wouldn't love that.  :)
Title: Re: Pitch detection
Post by: toneman on August 07, 2011, 01:40:14 PM
Well, if U are going to use a basic processor, U will still could use an "analog front end".
I like PICs, but if U want to use the DSP PICS, the following will not apply.

If U want pitch to voltage, the National LM2907 has been out for a very long time.
Used mainly for tachometers, it's a good linear frequency to voltage interface.
see here:
http://www.national.com/mpf/LM/LM2907.html#Overview

U probably could still use a good fundamental extractor/front end.
EH uses a "self-adaptive-threshold detector" in its Octave Multiplexer.
It's the parts just before the 4013.  The 4013 is the octave down maker.
Basically a flip-flop, divide-by-two thingie.
see here:
http://topopiccione.atspace.com/pjimages/EHOctaveMultiplexer.sch.gif

A while back, back in 2006, markusw and I and a few others were looking for an octave up to use with the
RingStinger clone.  We found several XOR-type circuits that all had capacitors in them.  Only worked over
a very narrow range of audio frequencies.  What we needed was an octave-up that wasn't capacitor-based.
I ended up getting a couple of cheap Behringer pitch shifters that did 2 octaves up AND 2 octaves down.

Good luck with your research!
8)

Title: Re: Pitch detection
Post by: ChrisA on August 07, 2011, 01:48:47 PM
Quote from: Taylor on August 06, 2011, 03:13:34 PM
[
Something along those lines can indeed be good signal conditioning before pitch detection (although filtering and squaring can be done much more easily in software than having to build a Big Muff), but how does that equal pitch detection? All the detection would still need to be done after that point to get a CV, varying resistance, or any controller based on the pitch of the signal.

The modern state of the art method is NOT to measure frequency.  You measure PERIOD and to get more accuracy you can average them.    This can also track "bends".

The way to measure period is first to "condition" the signal, detect the zero crossings.  This is pretty much the same this as "squaring"  Either way now you have a digital square wave

Next you need a fast counter.  Typically this runs 10MHz but it could be as slow as 1MHz for work with guitar.   The raising edge of the digital signal gates the clock to a counter, the next raising edge stops the counter.  Now you now the period.  If we assume a 1K audio input and a 10MHz counter we know the frequency to better then 1 milli-Hz and it only took one milli second.  Keep a running average of these counters.    Actually this was "star of the art" and therefor WAY to expensive to use in a stomp box in the 1970's.  Now a 1MHz is very easy and cheap to build.

The FFT method requires that you sample the signal for many cycles and then the frequency resolution is only as good as the block length.  You trade latency for resolution.  Period measuring give you any desired resolution in only one cycle and requires no memory or A/D conversion.

I have a device on a 4x5 inch PCB that costs about $50 to build that measures period at at the sub-nanosecond level.  That is gross overkill for this application.  You could live with 1000X less performance. So it should cost a LOT less then $50.


Title: Re: Pitch detection
Post by: potul on August 07, 2011, 04:56:10 PM
Quote from: ChrisA on August 07, 2011, 01:48:47 PM
The modern state of the art method is NOT to measure frequency.  You measure PERIOD and to get more accuracy you can average them.    This can also track "bends".
I already have a PIC based device that uses zero-crossing algorithm to track period, but I'm not satisfied with its performance, that's why I'm trying different approaches. I do have some limits due to the uC, I can't be sampling at 10Mhz.
Quote

The FFT method requires that you sample the signal for many cycles and then the frequency resolution is only as good as the block length.  You trade latency for resolution.  Period measuring give you any desired resolution in only one cycle and requires no memory or A/D conversion.
With an STFT I can get much higher resolution than the bin width using the phase difference information, but it is true that the bigger the FFT block the better. Whith autocorrelation I get exactly the same resolution I can get with zero-crossing, and it only depends on the sampling frequency. The problem I have is about the size of the sample I need to get results.

Anyway, your proposed method also has  a similar problem than the one I'm facing, because in order to get the period you need to wait at least one full cycle (around 12ms for a low E), and if you need to do some averaging you might need some more time.
Using autocorrelation I need between 1.5 and 2 periods of signal to get a reliable period for the lowest notes., which is not far away of the zero-crossing latency.

Mat.

Title: Re: Pitch detection
Post by: ~arph on August 08, 2011, 03:10:57 AM
Quote from: CynicalMan on August 06, 2011, 08:24:49 AM
I hate to be a downer, but Distortion with lots of low-pass filtering generally do pretty well at monophonic pitch detection. Play a Big Muff with Sustain set high. That gets the fundamental as a square wave pretty easily, and it doesn't get fooled by harmonics. Now polyphonic pitch detection would be sweet. Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.

How doesn't it get fooled by harmonics? for a narrow range yes, but not over the full range of frequencies (especially the low end)
Title: Re: Pitch detection
Post by: CynicalMan on August 08, 2011, 04:09:51 PM
In my experience, Big Muffs do a pretty good job with the low end. They don't have the issue that some fundamental detectors do with switching between a note and its second harmonic. If you hit a low E on a Big Muff, it stays there. YMMV.
Title: Re: Pitch detection
Post by: Gurner on August 13, 2011, 06:10:49 AM
Quote from: ~arph on August 08, 2011, 03:10:57 AM
Quote from: CynicalMan on August 06, 2011, 08:24:49 AM
I hate to be a downer, but Distortion with lots of low-pass filtering generally do pretty well at monophonic pitch detection. Play a Big Muff with Sustain set high. That gets the fundamental as a square wave pretty easily, and it doesn't get fooled by harmonics. Now polyphonic pitch detection would be sweet. Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.

How doesn't it get fooled by harmonics? for a narrow range yes, but not over the full range of frequencies (especially the low end)

agreed, the 2nd harmonic is especially strong on the lower frets/open strings.....it crosses zero, which completely screws traditional zero cross pitch detections.
Title: Re: Pitch detection
Post by: Ikedude on August 25, 2011, 12:23:45 PM
I ended up using a modified AMDF for my system like this. I got a little ambitious and built humbucker sized pickups so I could track each string individually. Here's a pic:

(http://img862.imageshack.us/img862/7338/midiguitar.jpg) (http://imageshack.us/photo/my-images/862/midiguitar.jpg/)

Don't be fooled by the USB cable in the background, it just has MIDI output, ha.


For more info on the AMDF here is a paper: http://www.asel.udel.edu/icslp/cdrom/vol2/218/a218.pdf (http://www.asel.udel.edu/icslp/cdrom/vol2/218/a218.pdf)

Basically compare the signal to a time (sample) shifted version of itself. A guitar is A LOT easier than a general purpose signal because you have a limited number of possible values (frets 0 - ~24). This algorithm uses subtraction and absolute-value, a little less computationally expensive than a full-blown FFT. I found accuracy was good for as little as 1.5 cycles and the computation time was on the order of ~2.5 ms. So delay for low E is around 20 ms.

I ran into problems with interference on the guitar and haven't quite figured out the best shielding arrangement. In the meantime I mounted this one on my bass:

(http://img576.imageshack.us/img576/1426/midibass2.jpg) (http://imageshack.us/photo/my-images/576/midibass2.jpg/)

Main problem here is the period length, lag on the order of ~30 ms is very noticeable. I was trying to figure out a good way to do a half-cycle AMDF... no luck yet, ha.

I used a PIC24FJ16GA002 at 32 MHz to do all the sampling, computation, and communication. Other circuitry is just to filter and shift the AC signal.

I'd be happy to share the code but I don't think its entirely legible, ha.

Title: Re: Pitch detection
Post by: Taylor on August 25, 2011, 04:06:50 PM
Googily moogily. I want one for bass. Any chance you'll make boards available?
Title: Re: Pitch detection
Post by: Ikedude on August 26, 2011, 12:03:59 PM
I ordered the boards through Golden Phoenix so I have way more than I would ever use. If you're comfortable doing SMT soldering (I'm not so I used stencils and solder paste) and have a way to program PICs (the five pins are for a PICKit), then we could probably figure something out. I got busy lately and haven't had the time to mess with it, so its still pretty rough around the edges.

I was motivated to build this to try and use it with Rock Band 3's Pro Mode, so right now its more of a fret-detection device than it is a pitch detector.

Title: Re: Pitch detection
Post by: zeptin on August 27, 2011, 03:57:53 AM
Well, I for one would love to see the code and board schematic :icon_biggrin:

I assume the cylindrical objects are the pickups you made, how are they constructed? Pickup wire wrapped around small magnets? How are they mounted on the board?

I have a lot of other questions, but maybe the code will clarify some of them :icon_smile:
Title: Re: Pitch detection
Post by: Ikedude on August 27, 2011, 11:54:58 AM
I don't really have a place to host the code. I'm not sure I ever did a schematic, but here is the layout.  Its a lot easier to find a place to host pictures than anything else, haha.

(http://img844.imageshack.us/img844/5338/midiguitarlayout.png) (http://imageshack.us/photo/my-images/844/midiguitarlayout.png/)

At first I was going to wrap my own coils but that didn't work out very well. Without a dedicated winder its really hard to keep good tension on magnetic wire around 3/16" steel rods.

I ended up using these for the guitar:
http://search.digikey.com/scripts/DkSearch/dksus.dll?Cat=196627&k=M10029-ND (http://search.digikey.com/scripts/DkSearch/dksus.dll?Cat=196627&k=M10029-ND)

I needed a lower profile for the bass since I wasn't gonna drill a pocket for tests:
http://www.newark.com/bourns-jw-miller/rl262-153j-rc/choke-15000uh-20ma-5-0-5mhz/dp/86K3337?Ntt=RL262-153J-RC (http://www.newark.com/bourns-jw-miller/rl262-153j-rc/choke-15000uh-20ma-5-0-5mhz/dp/86K3337?Ntt=RL262-153J-RC)

If you look at the datasheets for those you'll see they have a little pocket on the bottom. This was the perfect size for these:
http://www.kjmagnetics.com/proddetail.asp?prod=D21B-N52 (http://www.kjmagnetics.com/proddetail.asp?prod=D21B-N52)

It was a lot easier to mount the inductors than to try and figure out a way to mount some steel rods. There are two coils for each string wired in series. Like a humbucker they are reverse-wound (in this case that means I rotated them 180 to swap the leads) and the magnets on the bottom have opposite polarities facing up. They are pretty quiet but also sensitive to multiple strings on the guitar since they are so close. Maybe a strip of steel between each pair of coils would help isolate them, but I'm not too good with magnetics so I'm not sure.


Title: Re: Pitch detection
Post by: Hides-His-Eyes on August 27, 2011, 01:05:21 PM
Just so you know, that's a REALLY clever way of making a pickup. Bravo.
Title: Re: Pitch detection
Post by: Taylor on August 27, 2011, 04:50:46 PM
I agree. Really impressive, especially since your ultimate goal was to be able to play Guitar Hero with it.  :D
Title: Re: Pitch detection
Post by: potul on August 27, 2011, 05:15:17 PM
Quote from: Ikedude on August 25, 2011, 12:23:45 PM
I ended up using a modified AMDF for my system like this. I got a little ambitious and built humbucker sized pickups so I could track each string individually. Here's a pic:

Wow, I'm impressed (and inspired) by your pickups....

AMDF is in fact a kind of autocorrelation and very similar to the algorithm I use for low notes. But I face the same issue you mention, I need at least one cycle sample before being able to calculate accurately the freq, and for the low notes this is around 20ms of data. (for a guitar... bass is even worse).

If you ever find a "half cycle AMDF" let us know!
Title: Re: Pitch detection
Post by: AxeArt on August 28, 2011, 03:08:56 PM
Quote from: CynicalMan on August 06, 2011, 08:24:49 AM
Getting a chip that could track polyphonically would open up the door for a huge range of fun effects.

Well, in fact, these guys are doing just that: www.blackboxpedals.com/products (http://www.blackboxpedals.com/products) 
I met the designer a few weeks ago at the NAMM gear show in Nashville.  He's pretty much
done what you guys are talking about and created a wah pedal that listens to what the guitarist
is playing and operates a virtual rocker pedal in an aesthetically acceptable manner. 

I'm pretty sure it uses an FFT and a digital pot to pan the usual inductor based band pass
filter.  Pretty box too. 

AxeArt
Title: Re: Pitch detection
Post by: potul on August 29, 2011, 06:32:25 AM
Quote from: Ikedude on August 27, 2011, 11:54:58 AM
I don't really have a place to host the code. I'm not sure I ever did a schematic, but here is the layout.  Its a lot easier to find a place to host pictures than anything else, haha.

(http://img844.imageshack.us/img844/5338/midiguitarlayout.png) (http://imageshack.us/photo/my-images/844/midiguitarlayout.png/)


BTW, what kind of magnets did you use? Where are they located? I don't see them in the picture...
Title: Re: Pitch detection
Post by: Ikedude on August 29, 2011, 09:10:17 AM
QuoteIf you look at the datasheets for those you'll see they have a little pocket on the bottom. This was the perfect size for these:
http://www.kjmagnetics.com/proddetail.asp?prod=D21B-N52

Those are the magnets I used.

Title: Re: Pitch detection
Post by: jk-fm on August 29, 2011, 10:11:41 AM
Yamaha trained a neural network to do half-wave pitch detection.
Title: Re: Pitch detection
Post by: zeptin on August 29, 2011, 01:42:51 PM
Quote from: jk-fm on August 29, 2011, 10:11:41 AM
Yamaha trained a neural network to do half-wave pitch detection.

That sounds really fascinating, do you have a link to any further information?

And Ikedude, you could always throw a zip file up on rapidshare or something similar. If you don't want anyone and everyone to get hold of it you could put a password on the archive and PM the password to any interested people. Like me :icon_smile:

Anyway, that's a nice-looking layout. What are A1 and A2? Op amps?
Title: Re: Pitch detection
Post by: BlackBoxPedals on August 29, 2011, 03:15:28 PM
You can track multiple notes with an FFT and a decent DSP with only
about 100ms delay on most of the guitar spectrum (lower notes are
harder).  You can't do midi, but you can do a lot of cool stuff.  Potul,
is definitely on the right track with the combination of FFT, STFT et al,
but trying to get to sub 50 ms is going to be tough. 

Great stuff, though.  Love Ikedude's pickup. 

-BlackBoxPedals
Title: Re: Pitch detection
Post by: jk-fm on August 29, 2011, 06:28:23 PM
Here's one of the Yamaha patents:

http://www.google.com/patents/about?id=o_klAAAAEBAJ

Looks like they bought it from Axon/Andras Szalay.

Axon's "Early Transient Recognition":

http://www.axon-technologies.net/index.php?name=EZCMS&menu=17&page_id=28

Szalay is working on a new wireless guitar MIDI system with Fishman:

http://www.youtube.com/watch?v=5WCIXxU-mr8
Title: Re: Pitch detection
Post by: potul on August 31, 2011, 09:33:48 AM
Quote from: jk-fm on August 29, 2011, 06:28:23 PM
Here's one of the Yamaha patents:

http://www.google.com/patents/about?id=o_klAAAAEBAJ

Looks like they bought it from Axon/Andras Szalay.

Axon's "Early Transient Recognition":

http://www.axon-technologies.net/index.php?name=EZCMS&menu=17&page_id=28

Szalay is working on a new wireless guitar MIDI system with Fishman:

http://www.youtube.com/watch?v=5WCIXxU-mr8


thanks for the info! a lot of useful information in the patent document!

Interesting things I spotted:

-The algorithm is able to track the plucking point in the sting. This opens up for very interesting applications.... (Imagine a different MIDI instrument used depending on where you pluck). Unfortunately, from what I saw it will only work with hexaphonic data.... (I need to dig more into it...).
-They use 2 separated algorithms to track pitch: A fast one using neural network and peak detection,  and a second one (slower but more stable) using zero crossing averaging 3 periods. The zero crossing results are also fed into the neural network.
-They use the neural network to determine pitch and plucking position faster, but still looks to me they need a full wave to do it, based on the description of the algorithm.
-The zero crossing algorithm does 2 interesting things: Interpolation to have a higher time resolution, and use of the slope at each zero to determine how big the following peak will be. It also uses a zero point reduction process in order to keep only the relevant ones based on an envelope tracking. Looks interesting.
-There is a description as well around quantization and how to handle bends that can be useful.

thanks a lot for the information!

Mat
Title: Re: Pitch detection
Post by: defaced on August 31, 2011, 10:01:51 AM
QuoteMaybe a strip of steel between each pair of coils would help isolate them, but I'm not too good with magnetics so I'm not sure.
Check out the pickup winders forum over a the Music Electronics Forum.  There are a ton of threads on theory, and you can also post your application and get some good advice. 
Title: Re: Pitch detection
Post by: Ikedude on August 31, 2011, 11:12:34 AM
A1 and A2 are opamps, and both have 4 separate circuits.

The Yamaha stuff is interesting, thanks! I've been trying to think of a way to do half-cycle detection but I don't think I can with this PIC. Right now the time between samples on each string is set to be the difference in period between the two highest frequency notes I expect to see on each string. To be able to have the time resolution to distinguish between half-cycles I'd need to double, or maybe quadruple (depending on the algorithm), the current sampling rates.

I read the pickup makers forum regularly but the couple of times I've posted I didn't get much of a response. Maybe I'll try again.
Title: Re: Pitch detection
Post by: zeptin on August 31, 2011, 12:02:02 PM
Well, if you're looking for a processor with a little more horsepower, the dsPIC33FJ128GP802 can do 40Mhz, and its ADC can sample at least twice as fast as the 24F you're using. I think potul's used it quite a bit, he can vouch for it. I also recently discovered that there's a dsPIC33E series which can go up to 60 (!) Mhz. They also include USB capability, which I eventually want to try to use to make a cheap recording interface for a PC. So far I've only got audio loopback plus a little FIR filtering working on a dsPIC33F, but it's still early days. I'll probably post more information if/when I can get something workable.
Title: Re: Pitch detection
Post by: joenoone on September 27, 2011, 10:15:39 AM
Wonderful work you've been doing - I am definitely interested in the problems/solutions you have with implementing this for bass as I'm a 5-string player (low B) and have been dreaming of a better system.

Have you looked at the ARM platform? I see that Arduino is planning to create an ARM version soon (the http://www.wired.com/gadgetlab/2011/09/arduino-arm-products (http://www.wired.com/gadgetlab/2011/09/arduino-arm-products)) so lots of tools there, and I've seen other projects/platforms that use ARM processors that are fairly inexpensive. Also have you had a look at the XMOS platform (http://www.xmos.com/ (http://www.xmos.com/)) which has some serious horsepower?

That said, I'm guessing the problem is more with the algorithm than the processor in terms of latency.

For places to post your code, there are sites like pastebin or dropbox you might look at.
Title: Re: Pitch detection
Post by: earthtonesaudio on September 27, 2011, 07:56:19 PM
That pickup design is a brilliant project in itself.  Might be able to improve it for guitar by staggering the strings so two pickups are not so close to each other.  Regardless, a very nice application for available components.
Title: Re: Pitch detection
Post by: Ikedude on January 30, 2012, 03:57:57 PM
This might be a little overdue but...

If anyone is still interested in the code:
http://dl.dropbox.com/u/10464544/MIDI%20Pickup%20Source.zip (http://dl.dropbox.com/u/10464544/MIDI%20Pickup%20Source.zip)

I went through and added some comments where I though it might not be obvious what I was doing (was a good exercise to see if I knew what I was doing, ha).

If you have any questions, feel free to ask.
Title: Re: Pitch detection
Post by: ~arph on January 30, 2012, 04:27:22 PM
Hi,

Thanks for the source, I will look into it as me being a software engineer it's always nice to see some code that has audio elements in it opposed to the mostly boring code I see each day  ;D

I don't think I'll build the pickup, but congrats for the great design! and thanks a lot for sharing it with us.
Title: Re: Pitch detection
Post by: zeptin on January 31, 2012, 03:53:34 AM
Thanks for the code, I'm sure that going through it will be quite educational :icon_biggrin:

I don't have any questions yet, as I still want to try to implement AMDF myself. But hopefully more people will come forward with algorithms or improvements of their own.
Title: Re: Pitch detection
Post by: joenoone on February 07, 2012, 09:21:38 AM
Hey Potul - are you still working on this? If so I ran across this a few weeks ago that might help :

    Nearly Optimal Sparse Fourier Transform - http://arxiv.org/abs/1201.2501

I found a reference to it from Slashdot - http://science.slashdot.org/story/12/01/20/037236/faster-than-fast-fourier-transform

"MIT news reports on research done resulting in a Faster-than-fast Fourier Transform algorithm. 'At the Association for Computing Machinery's Symposium on Discrete Algorithms (SODA) this week, a group of MIT researchers will present a new algorithm that, in a large range of practically important cases, improves on the fast Fourier transform. Under some circumstances, the improvement can be dramatic — a tenfold increase in speed. The new algorithm could be particularly useful for image compression, enabling, say, smartphones to wirelessly transmit large video files without draining their batteries or consuming their monthly bandwidth allotments.'"
Title: Re: Pitch detection
Post by: PRR on February 12, 2012, 05:51:15 PM
> If you ever find a "half cycle AMDF" let us know!

Spoiler:

Can't be done for most musical instruments because the waveform is not symmetric.

Play nominal A=440.

One whole-wave is 2.272727mS.

One half-wave computes as 1.136363mS.

If the wave is 45:55 asymmetric, the first half-cycle is 1.022727mS which computes to 489Hz. The next half-cycle is 1.25mS or 400Hz. Or conversely, 400Hz and 489Hz. ~~4 semitones different, or +/-2 semis from the full-wave pitch. Your half-cycle measurement could give either 400 or 489 depending on pluck-direction or pickup polarity.

You may be able to do better by tracking the 2nd and 3rd harmonics. They full-wave sooner. They will in-general (strings or pipes) not be exact harmonics; but may be a heap closer than +/-2 semis. (I guess this is what the FFT does?)
Title: Re: Pitch detection
Post by: Ikedude on February 21, 2012, 02:23:40 PM
Does anyone know of a good resource that delves into the harmonic content of a plucked string?

I'm hoping to find out that the relative magnitude of the harmonics are consistent, at least right after plucking. This probably isn't the case but I can't find much helpful information.
Title: Re: Pitch detection
Post by: artifus on February 21, 2012, 02:28:39 PM
Harmonic Content of a Plucked String (http://lmgtfy.com/?q=+harmonic+content+of+a+plucked+string)

https://ccrma.stanford.edu/realsimple/harmonics/ (https://ccrma.stanford.edu/realsimple/harmonics/)
Title: Re: Pitch detection
Post by: Ikedude on February 21, 2012, 02:59:20 PM
Nice, I saw that but it doesn't delve into the numerical relationships between magnitudes. Say, "for a low E string when its plucked open the magnitude of the 2nd harmonic is roughly equal to the first".

It talks about how the harmonic structure of the sound can be changed and how a guitar can then sound like a clarinet but it doesn't say what it is about the guitar signal that makes it a guitar.

I'm guessing the pluck and pickup position affect the nature of the signal too much to get any kind of consistent result. Maybe if you always struck it the same way, ha.
Title: Re: Pitch detection
Post by: gritz on February 23, 2012, 11:34:42 PM
The CCRMA stuff is great for theory (especially if you're the sort of person that would skip guitar lessons in favour of math class), but it is pretty light on practical application and the kind of creative nuance that music is all about. There, I said it.

You're absolutely right about the pluck and pickup positions being an essential consideration. A string can simultaneously vibrate in many modes and which modes are created and detected depend on the positions of the pluck and pickup respectively. Imagine "pinging" the harmonic at the 12th fret. What you're doing is holding the string still at that point, so it effectively becomes two strings of half the lenght which naturally vibrate one octave up. If there was a pickup at the 12th fret there would be very little output as the string isn't moving at that point - it's what's called a node. If you pluck an open string at the 12th fret you get a very mellow tone because it's mainly fundamental and there's one point of maximum vibration -in the centre of the string. Moving the pluck away from the centre of the string creates more complex vibration modes. There's a lot of complicated maths out there, but it's really just dividing the string into fractions:

http://labspace.open.ac.uk/mod/resource/view.php?id=437040 (http://labspace.open.ac.uk/mod/resource/view.php?id=437040)

Knowing this doesn't make a pitch detector any easier to build though. The human ear seems pretty good at guessing the fundamental from the (regular) spaces between the harmonics, but squeezing the Fourier transform into a pedal isn't easy for a hobbyist either...

Title: Re: Pitch detection
Post by: Gurner on February 25, 2012, 06:48:17 PM
Quote from: Ikedude on February 21, 2012, 02:59:20 PM
It talks about how the harmonic structure of the sound can be changed and how a guitar can then sound like a clarinet but it doesn't say what it is about the guitar signal that makes it a guitar.

Slightly off topic .....from a human perspective (vs a harmonic perspective which is being discussed here) it's mainly the attack portion of an instrument that humans recognise/differentiate instruments by - Roland sussed this many moons ago & used it to great effect when RAM was still mega expensive...basically they sampled the attack 'signature' of all the different instruments (flute chiffs. violin 'bows', guitar chugs, piano hammer/stikes etc)...used that mini sample as a 'header'...and then slapped a synthesized tail on the back end of it....end result very convincing at with very little memory used. (the technique was called LA synthesis by Roland - http://en.wikipedia.org/wiki/Linear_Arithmetic_synthesis)
Title: Re: Pitch detection
Post by: bilwit on March 02, 2012, 05:39:01 PM
Definitely interested in this. I'm planning on looking into Autocorrelation with my dsPIC33F in the near future.
Title: Re: Pitch detection
Post by: potul on March 04, 2012, 03:08:03 AM
Quote from: joenoone on February 07, 2012, 09:21:38 AM
Hey Potul - are you still working on this? If so I ran across this a few weeks ago that might help :

    Nearly Optimal Sparse Fourier Transform - http://arxiv.org/abs/1201.2501

I found a reference to it from Slashdot - http://science.slashdot.org/story/12/01/20/037236/faster-than-fast-fourier-transform


Thanks for the information, but I doubt I will be able to use it without a huge investment in ASM programming... :)
The good thing about using FFT in dsPIC is that there are optimized routines in C that can use all the powerhorse of the uC

One example.... I tried to do a "custom" autocorrelation algorithm to stop calculating when a match is found, and it worked, but it took much longer than simply running the built in correlation in C. I'm not saying it can't be done, but it's out of my ASM capabilities.

BTW, just one warning... if you ever try to use the correlation function in dsPIC be aware that there are some wrong pieces of information in the datasheet. It took me some time to troubleshoot.