Forums

Fundamental frequency of a Guitar string

Started by DONVOTH 2 years ago29 replieslatest reply 2 years ago307 views
I am using a Zilog 8 bit Micro running at 20Mhz. I am trying to determine the fundamental frequency of a guitar string. I have turned the complex wave into a series of pulses so that the micro can count the period of the incoming wave. I am having a problem with the lower frequency signals. It seems these signals have a significant second harmonic content that is causing my period count to be an octave to high. I am looking for advise as to how I can eliminate this problem. I know that my approach can function since Roland Corp is using a 8 Bit 10Mhz processor to do the frequency counting in their Guitar tuner. So we know a DSP and Fourier Transformations are not required. Any advice would be greatly appreciated.
[ - ]
Reply by trogers531June 30, 2018

OK, so there’s a few things here...

Firstly, the ‘harmonics’ aren’t harmonics in the classical sense; they’re ‘partials’, and their frequency isn’t necessarily related in a nice, clean integer fashion.

That said, there is every reason (as long as we’re talking about guitars) to expect a reasonably accurate tuning using the string’s harmonic, the octave note obtained by plucking the string whilst gently touching it at its midpoint.

This trick suppresses some of the trickier artifacts of processing the resonant open string, but it begs the question: your device should be able to register a low E, or it’s not really that useful. But the interfering frequencies are not guaranteed to be integer multiples of the fundamental, especially if your strings are getting a bit old; out-of-tune partials is one of the earliest signs that a new set is due.

Secondly, lets talk about your ‘pulses’. It’s assumed you’re making some effort to transfer from the analog to the digital world, even if it’s just pumping such a large signal into a GPIO pin that it approximates a digital input (with appropriate clamping, of course).

A much better approach is to amplify the usual small microphone output and convert it with additional circuitry onto a form your micro can process. Two paths to this exist, generally speaking: an appropriately configured comparator circuit, or an A/D converter.

Assuming you’ve chosen the former solution, you’re now entering into the mysterious world of correctly identifying the peaks, or their dual, the ‘zero crossings’ (as if zero really exists, right?). 

This is a problem that has vexed sonar and radar engineers from day one; if you expect that a real world signal will cross some arbitrary threshold, the Universe will quickly shatter your illusions with signals that vary, or remain far too low to identify reliably, or any of a dozen other little obstacles that reality has ready for you.

There are some ways to approach these problems, but in general the latter design approach has been far more useful - use an A/D, bite the bullet and get some deeper signal processing into the mix. The cost of outragiously powerful, power miserly 32 bit chips isn’t much more than the hardware you’re considering.

There is a useful trick, if you can constrain the problem to dealing with pulse widths that are variously longer (fundamental length) and shorter (harmonic length); you take a buffer of measurements and sort through them, adding or otherwise dealing with shorter measurements if they’re somewhere near half or one third the longest measurements, then process the resulting bunch of corrected measurements.

Sort of a magic filter, not too far from an expert systems type of AI algorithm, because you need to get a lot of experience with what the data buffers look like, and encode savvy guesses as to what the numbers really represent.

Finally, let’s talk about what you get when you’re done analysis. Presumably, you want some valid, reasonably close metric of ‘in-tune-ness’, represented perhaps by the note (E, F#, G, whatever) and some number that shows how close to the perfect target your string is.

That number is conventionally expressed in cents, or hundredths of a chromatic half-step, the distance between E and F, B flat and B, and so on. 

Mathematically speaking, the chromatic distance from one octave to the next is a factor of two, so you multiply the frequency of the root note by 2 to find the frequency of the octave. Similarly, the distance from one note to the next a half step higher must be the twelth root of 2; multiply the frequency of the root by the twelth root of 2, and you get the frequency of next higher chromatic note, do it again for the next, and true to it’s name, do it twelve times and you get the octave, back to a factor of two.

I’m sure you can see where this is going: some very long strings of numbers that are almost equal to 1. One cent is a distance that is obtained by multiplying the root by the 1200th root of 2, and if you want to work in a domain a factor of 10 more precise, so that you can keep rounding and other math errors from creeping into the displayed final value, you need to work with the 12,000 root of two.

Lest you think this sort of precision is overkill, note that the standard for ‘in tune’ is usually considered a note within a few cents of true frequency at most. More than +/- 3 cents and you can begin to hear too many beats and off sounding harmonic relationships with notes that should be nice and well behaved, fourths and fifths and thirds and such, relationships the characteristics of which are baked into our heads, probably many millennia ago. 

There are useful clues out there to how you can proceed without stumbling into territory that begs for a Cray-class processor. Ages ago Peterson and Conn made do very nicely with electromechanical solutions that exploit the fantastic sensitivity of the human vision system for high quality measurements, about both of which you can find lots of information out there on the internet.

(Peterson is the last man standing nowadays, with very cool equipment and by far the best tuner app available, iStroboSoft, about $20 and worth far more. Check it out for a lesson in great app design; visually, they’ve made it work just like their original very expensive box, and it is really a stunningly informative display that is so simple in concept it’s scarey.

Finally, don’t forget the other input to your processing, the reference clock you’re using to make your measurements. The StroboConn used the 60 Hz line, as I suspect Peterson’s early designs did also, good enough by far for some of the most common uses: if everybody in the band is tuned up with the roadie’s magic box, they’re all in tune even if the local power company is a few millihertz short of 60. 

(This is why a lot of early rock stuff is way off of standard tuning, like a bunch of Allman Brothers concert recordings - they tuned to Greg’s Hammond B3, which uses tone wheels driven by a synchronous motor as a frequency source.)

OK, enough. Hope that helps frame the task you’ve set for yourself. Good luck, and ask if any of it wasn’t clear.  —Tom

[ - ]
Reply by JackCrensJune 29, 2018

If you are getting such strong second harmonics, it could be because the signal itself is highly nonlinear.  Have you looked at the signal on a scope?

Also, most guitar amps are set up to create lots of harmonics.  That's because listeners like the "fuzz." 

Thirdly, every input needs some filtering. It's not a good idea to just feed the raw signal into an A/D converter.  There should be at least an R/C filter first. I realize that this is tricky, since you want to (I presume) tune over a fairly wide range, and the second harmonic is not that far from the fundamental.


Fourthly, there could be some kind of resonance going on. This happens when the sampling frequency is an even multiple of the signal frequency.

Finally, have you considered a phase-locked loop (PLL)?  I realize that's probably not the simplicity you'd like, but PLL's are _VERY_ effective.



[ - ]
Reply by DONVOTHJune 29, 2018

I have limited the frequency range to 60 hz and 6K with Link Witz Ryle filters. That does not help with a harmonic on the open E string since that is easily with in that window.

I am trying to do this without doing a A/D conversion. I am just monitoring pulse widths.

I am courious how PLL would help me? It has been many years since I have worked with a PLL.

[ - ]
Reply by artmezJune 29, 2018

While I am unfamiliar with the harmonic behavior of a plucked string, I do know a bit about harmonics generation in equipment. Second harmonics can be caused by both cross-over distortion (from op amps, old telephone carbon mics, and CMOS analog switches on single polarity supplies), rectification, or clipping and/or compression. Your entire signal chain should be examined for any of these. Some micro based (unipolar) ADCs can also generate some 2nd harmonics from the crossover distortion of its MS bit. Some of these generate infinite harmonics (e.g. rectification), but higher order harmonics tend to taper off in amplitude quickly.

[ - ]
Reply by DONVOTHJune 29, 2018

The harmonics are an integral part of the original signal. I can filter them out that the high end of the guitars frequency range but not at the bottom end.So I need a way to identify them in the software and then remove those pulses that represent the second harmonic so that only the fundamental pulses remain. Any ideas??

[ - ]
Reply by JackCrensJune 29, 2018

Donvoth, you said "The harmonics are an integral part of the original signal."

That sounds wrong to me. I thought you were building a tuner to let you tune the guitar string to a desired frequency.  I can't imagine any way you could do that if the original signal is full of harmonics.  I must be missing something. 


I also don't understand your comment that you are trying to measure pulse widths without using the A/D. To measure a pulse width, don't you need to detect zero crossings?  How can you trigger on zero crossings if you aren't measuring the voltage???


Color me confused.  

Re the PLL: it's an analog circuit with a voltage-controlled oscillator. It's been awhile since I used one, but the general idea is that if the local oscillator is anywhere near the same frequency as the signal, it feeds back an error signal that adjusts the local oscillator's frequency to match the signal.

PLLs are very useful when you're trying to decode a signal that may or may not be transmitting on the right frequency.

[ - ]
Reply by dszaboJune 29, 2018

Guitars do have a fair amount of harmonic content, not too much can be done about it. If you’re serious about it, when you tune you can put your guitar in the neck position and roll back the tone so it’s mostly low frequency content, but generally nobody does it that way.

Using an ADC with signal processing is still relatively modern. Back in the day, electronic tuners would shape the signal with linear filters to reinforce the fundamental and distort the hell out of it. You could then use F/V converters to measure the time between edges to get the pitch.  Nowadays, that technique is fairly antiquated, and there are much more accurate ways to measure pitch.

I’ve heard of PLLs being used to measure pitch for synthesizers, but they don’t work very well because of the harmonic content.  Note tracking only sort of works, and as soon as you put a chord through it it falls apart real fast.  I think for pitch detection, a PLL is a bit heavy handed, as the frequency can be measured directly with a timer capture

[ - ]
Reply by DONVOTHJuly 2, 2018

What do you mean with "timer Capture"? I think I am already doing that but the harmonic content on open strings is messing up the data.

[ - ]
Reply by dszaboJuly 2, 2018
https://en.m.wikipedia.org/wiki/Input_capture

a very common MCU peripheral for measuring timing of digital waveforms.  The only way to attenuate the harmonic content is to low pass filter it

[ - ]
Reply by TheCaptainJune 29, 2018

If your project is about synthesising sound rather than the measurement of the sound, you could use a tuner or spectrum analyser app on a smartphone.

[ - ]
Reply by DONVOTHJune 29, 2018

This is interesting. I am developing a standalone product. How can I integrate a tuner app into my product?

[ - ]
Reply by TheCaptainJune 29, 2018

Have a look at Arduino.  I am sure that someone else has already done this.

[ - ]
Reply by dszaboJune 29, 2018

Given your constraints, I’m not sure you have a ton of options. I’d start by adding a low pass or shelf filter to accentuate low frequencies

[ - ]
Reply by dszaboJune 29, 2018

Here’s a chincy one with a first order low pass at about 330Hz to get around the problem you are experiencing:  http://www.muzines.co.uk/articles/guitar-tuner/2656

[ - ]
Reply by artmezJune 29, 2018

You said you're measuring the "period", so it seems you are doing this digitally, right? Then an issue that can occur for low frequency digital inputs is their limitation for slow rise times that can cause jitter. This is typically "fixed" with a digital Schmitt trigger (e.g. 74HC14), or comparator with hysteresis (essentially the same thing). Have you tested your "counter" with an external square wave generator at that frequency? If not, try that and if that doesn't work, then there may be an issue with how you're capturing the input. Don Lancaster's TTL (and CMOS?) handbooks have a good circuit for a "resynchronizer" for doing "clean" frequency measurements (pages 208-211 in the first edition I have).

I also found a Wikipedia article for "String harmonic" that mentions that a string generates a bunch of harmonics (I guess since a string pluck is wide-band like an impulse) resonating over the length of the string (or fretted position). No indication of how amplitude of harmonics relative to fundamental like the standard Fourier series equations of an ideal square wave with its odd-only harmonics (e.g. 1, 3, 5, ..).

[ - ]
Reply by dszaboJune 29, 2018

A guitar string is more of an acoustic system and the models representing it are more accurately modeled by a forced traveling wave, but it can be simplified by modeling it as a resonant linear system with some sort of impulse.  The complexity arises because the timber of the signal is affected by a lot of factors, including string gauge the location of the attack, the material/speed/angle of the plectrum, where the string activated.  In fact, if you hit a string hard enough, it will go sharp (higher in pitch) which is inconsistent with a linear model.  I would contend that even the harmonic series isn’t spot on, but only pretty close.  All this is why you would have trouble finding a definition for the harmonic amplitudes

[ - ]
Reply by DONVOTHJuly 2, 2018

My software does exactly what is expected with and oscillator input rather than a guitar input/

[ - ]
Reply by jbrowerJune 30, 2018

Donvoth-

As other answers have mentioned, converting from a 60 - 6000 Hz time domain waveform to "a series of pulses" is key.  This conversion is the fundamental, root aspect of your system that determines its success or failure. Can you grab data from your uP and plot it side-by-side with the original waveform ?  If so then you can make sure this comparison shows a reasonable and consistent relationship -- for example a crude PWM or linear or log PCM approximation.  Until you reach that point, I would respectfully and politely say, based on your post, that you cannot yet trust your pulse data, and you might be counting aliased pulses, or missing pulses needed for a correct answer.

Some searching on Roland's underlying method implies a custom chip or FPGA processing prior to the 8-bit slow micro.  This would make sense -- very cheap, highly specialized hardware based conversion to a frequency domain approximation the micro can deal with.

-Jeff

[ - ]
Reply by DONVOTHJuly 2, 2018

Some of what you are saying I am not following. How ever I can tell you that the Roland Product has nothing in the signal path in front of the 8 bit micro except analog filtering and gain boosting. No custom chip no FPGA.

[ - ]
Reply by jbrowerJuly 2, 2018

Donvoth-

Are you able to see the part number of the Zilog micro ?

-Jeff

[ - ]
Reply by DONVOTHJuly 3, 2018

The micor Roland is using is  MB95F108AN

[ - ]
Reply by jbrowerJuly 3, 2018

Donvoth-

That series of Fujitsu uP has a 10-bit A/D converter:

http://www.fujitsu.com/downloads/MICRO/fma/mcu/ds9...

My guess is that Roland uses this, acquires a few frames of 512 or 1024 points at a sampling rate around 22 kHz, FFTs those frames, and averages them.  It doesn't have to be fast -- we're not talking real-time speech processing.  It's just guitar tuning.

Lots of good suggestions for low-tech, ultra-cheap methods on this thread, but I highly doubt Roland is doing anything but the most simple, straightforward method needed for a reliable, robust consumer product.

-Jeff

[ - ]
Reply by DONVOTHJuly 3, 2018
I know nothing about FFT routines. Where can I learn about that? Can I buy the code mostly done and just have to message it in to my code?
[ - ]
Reply by dszaboJuly 3, 2018

Whoa now...  Let's ease up a little bit.  I'm a little surprised to see so many people advocating for such a heavy handed approach.  I think you are already on a decent tack and with proper filtering in the analog domain, you should get reasonably good results.

I'd also point out that pitch detection with spectral domain data isn't a "Let's use a magic FFT" type of problem.  If you aren't comfortable with those types of maths, it stands to reason that you would spend a lot of time chasing your tail.  A lot of really bright people have worked on this problem, and unfortunately it isn't the kind of thing that has a straight forward answer.

If you really want to get into some DSP signal processing, you'd do well to start with a google search for Pitch Detection.  Also, here's a couple good links to start:

https://ccrma.stanford.edu/~pdelac/154/m154paper.h...

http://miracle.otago.ac.nz/tartini/papers.html

I'm particularly fond of McLeod's writing style, he is very clear and provides a good level of detail.

[ - ]
Reply by DONVOTHJuly 4, 2018
I would rather not do DSP FFT if I do not have to. So far I am finding that I can isolate the notes from the guitar that have a lot of second harmonic. I am doing that by creating an interval table that lists the intervals between adjacent pulses. I have found that when the second harmonic is strong then there are few unison intervals and more octave intervals. When this happens I can then filter out the second harmonic pulses from the original table and I get a table of mostly unison intervals and a pulse count that is at the fundamental. I have yet to prove this method is consistent enough to be useful.

If I understand you correctly you believe that FFT analysis is not essential to make this project work?

[ - ]
Reply by dszaboJuly 4, 2018

Good to hear you’re coming up with some clever trickery. Again, I would recommend doing more to condition the signal in the analog domain before converting it to a pulse train, but if you think you’ve found a way around, that’s great. Frequency domain techniques (ie FFT) are not required, and if you look at the links I shared you will find a wide range of techniques that are exclusively in the time domain.  Including what you are already doing, which is a zero crossing detector

[ - ]
Reply by DONVOTHJuly 4, 2018

I have started to look that those links. It seems there may be some useful stuff there. THANX Don Voth

[ - ]
Reply by jbrowerJuly 9, 2018

Donvoth-

I did this Google search:

  FFT algorithm for guitar tuning

and the whole first page has good links, including some pro and con discussion on whether to use an FFT algorithm and also code examples.  The dsp.stackexchange.com section, including the half-dozen or so sublinks listed after the first one, are excellent reading.

I would consider an FFT algorithm a way to get an overall picture of fundamental pitch and harmonics, verify your filtering works, and otherwise provide a sound basis for moving forward.  I don't know why anyone would think a simple FFT is to be avoided -- it's not a lot of code and it's an easy algorithm to debug.  From there the challenge would be to precisely pin down the fundamental pitch; i.e. increased frequency domain resolution in a small region of the spectrum, and as advocated by other answers, there are some clever ways to do that.

As I read back over this thread, my main concern is still how are you actually sampling ?  The Fujitsu uP used in the Roland product has a 10 bit A/D converter, and I think we can safely assume that's why they chose that device.  What is the micro you are using ?  I haven't seen any discussion yet about your physical sampling method, other than your analog filtering from 60 to 6000 Hz.

-Jeff

[ - ]
Reply by bholzmayerJuly 2, 2018

While I agree with Tom and the others sharing their experience and questioning if you can really expect only harmonic content , just some hints from my side:

often guitar tuners have either a preselection for the string which you want to tune/check - or they make a guess on which string it is (which rather often is wrong as long as the string is not closely enough tuned in).

Applying this to your problem means: if it's merely about the second harmonic content, you can nevertheless make an assumption about the tuning error and give either a measure or an indication for the user.
If you know which string, the harmonics should even help you because you get more zero crossings (and more accuracy).

While I could consider counting zero crossings as nothing else than a very bad (1bit) FFT on a digital signal captured with a very bad (1bit) ADC, I agree that this might lead to a sufficiently good result, but it requires that you have something to compensate. I guess you will end up with statistics:

- compare all possible (six) results for every string and select by probability
- improve the result by averaging over time
- use as much as possible user input (string select|intentional detuning|open tuning)

Bernhard