how about amplifying,low-pass filtering ( 3rd order), and clipping
the guitar signal with a cheapo 324 quad-opa. Generate the desired
reference freq. with the RTCC. Make a software-PLL style frequency-
comparator ( edge-triggered RS-flip-flop )which is set by ref.-timing
and reset by input signal . Connect a 'sharp' LED to the Fin>Fref port and
a 'flat' LED to the Fref>Fin port. As the input freq. comes up/
down towards the ref.freq. the flat/sharp LED will dim.
Just a quick idea; don't know if it makes sense.
Another idea, not mine , saw it on Frankfurt Musik fair about 10yrs ago:
A guy fed a bright LED with a microcontroller generated reference
frequency and used the stroboscobe effect to visually tune the strings
without making noise - nice !

>It would be interesting to also pursue the generation of MIDI from a
>guitar signal ....

and from other real world events. In my free-time ( not much) I currently
work on such a project : a microphone is placed at each end of a 1m plastic
tube. If you hit the tube with e.g. a drum-stick the mikes generate pulses
with a runtime delay. A 16C54 along with a 324 opamp digitizes these pulses
( MIDI key velocity ) and calculates a MIDI-note-# from the trigger delays.

I 'll put schematics, layouts + code related to MIDI, musicelectronics, PICs
and other controllers on my ftp site as soon as possible ( late summer ?)
Will be announced here and on MIDI.boards

> Another idea, not mine , saw it on Frankfurt Musik fair about 10yrs ago:
> A guy fed a bright LED with a microcontroller generated reference
> frequency and used the stroboscobe effect to visually tune the strings
> without making noise - nice !

> > Another idea, not mine , saw it on Frankfurt Musik fair about 10yrs ago:
> > A guy fed a bright LED with a microcontroller generated reference
> > frequency and used the stroboscobe effect to visually tune the strings
> > without making noise - nice !
>
> Oh man, I've gotta try it!
>
Must have to darken the room to tune?? Anyone have any ideas, helpful
hints to get this to work? Seems that it would be a rather stright
forward project!

Your suggestion for dealing with the harmonics is great. But my tuner
attempts to determine by itself which string is being tuned. I suppose
with expensive programmable filters you could detect the pitch, then dial
in the filter real quick to protect against the harmonics, which
intensify with time. But if you put in a filter sufficient to chop the
harmonics of the low E string (86 Hz or so) you'd never pick up the high
E two octaves up.

My hackbuddy and I have decided to return to this project and see if we
can get it to work. The idea will be: count on the fundamental being
strong for the first several cycles (seems to be safe), then start
ignoring any transitions that aren't close to the expected timings. For
example, if you're tuning the 110 Hz A string, you'd initially get a 110
cycle square wave. Then as the vibrations decay, you'd start seening
double, triple, or some oddball combination. If I remember right, the
second harmonic is the most prominent and can be isolated. But I'll have
to look again. Anyway, it might be as simple as ignoring every odd
pulse, once the expected pitch is established. Owell I'll try it and
post the results!

ps: about the code I've offered to put up... I was going to make it
available via ftp this morning but I forgot to move the files. I'll do
it tomorrow when I'm dialed in from home.

David, here are a few suggestions for a solution to your problem with
harmonics:

1) Use a dual speed PLL to initially lock onto the strong fundamental
in 'fast' tracking mode and then automatically switch to 'slow' mode
to stay locked onto the decaying fundamental (decrease capture
range). The 'fast' speed would be optimized to lock onto the
fundamental quickly and the 'slow' speed would be fast enough to
track tuning variations but too slow to 'jump' over to a harmonic.
The fundamental could then be measured by coupling the vco to the
pic.

2) Use zero crossing detection. It has the unique property of allways
digitizing the lowest frequency, even if its not the strongest.
A well designed front end, and possibly threshold tracking to
maintain sensitivity could be used to extend its range.

3) Use a switched capacitor filter with external clock input configured
as a 'comb' filter. Lock on to the fundamental and feed it into the
filter's clock, thereby attenuating its harmonics. Something
creative would need to be done here to aquire the fundamental in the
first place.

4) Use the pic to implement an FIR or IIR filter.

5) Use the pic to integrate its measurement of frequency and then use
that to detect the rate-of-change. If the rate of change (slope)
exceeds a pre-set value then we know the detection circuitry has
switched from the fundamental to a harmonic. At that point in time
divide the new frequency measurement by the fundamental to calculate
the harmonic factor and use that factor in subsequent calculations.
This idea would work best if the signal were processed first by a
PLL to lock onto a single spectral component. In fact, you could use
the lock detection circuitry of the PLL to detect when the
switchover to a harminic occurs.

6) Can you tell if a note is sharp or flat from its harmonic as well as
from its fundamental? If so, then use a PLL front end to purify the
spectrum (one frequency at a time) and do your function on the VCO
signal.

7) Use an analog input and implement a PLL in pic firmware, using a
combination of the above ideas.

I really know absolutely nothing about music, but I hope these ideas are
usefull, or stimulate the formulation of new ones.

> 2) Use zero crossing detection. It has the unique property of allways
> digitizing the lowest frequency, even if its not the strongest.

It can't be that easy, can it? I'm already amplifying and heavily clipping
the signal. Isn't that the same thing? Maybe I need to read up on zero
crossing detection.

> 3) Use a switched capacitor filter [...]

Impractical but clever.

> 4) Use the pic to implement an FIR or IIR filter.

What are these?

> 5) Use the pic to integrate its measurement of frequency and then use
> that to detect the rate-of-change.

What I was planning was simpler. Once the harmonic kicks in I'll be able
to recognize it because transitions will suddenly arrive way too early.
I can simply discard transitions that aren't within a few percent of
where I expect to find them.

> 6) Can you tell if a note is sharp or flat from its harmonic as well as
> from its fundamental? If so, then use a PLL front end to purify the
> spectrum (one frequency at a time) and do your function on the VCO
> signal.

How does one "purify a spectrum" with a PLL? What does that mean? But
yes of course it is possible to track changes in a harmonic as readily as
the fundamental.

> 7) Use an analog input and implement a PLL in pic firmware, using a
> combination of the above ideas.

I've done this for another project and it worked *very* well. However I
was able to isolate the fundamentals with analog filters ahead of time.

This has some possibilities. But you need to guess the reference freq.
from early edge crossings to get the correct string. Help me out now,
what would the algorithm look like in pseudo-code?

wait for an edge crossing.
start a timer.
next edge crossing is 1/2 period.
estimate string, get reference period, set up reference period in RTCC.
loop
wait for an edge or RTCC timeout.
if edge
set sharp LED. ;; you'd want to re-sync the RTCC?
if RTCC
set flat LED. ;; you'd have to re-sync the RTCC to the next edge?
if (too many RTCC's with no edge)
break, restart

So when you're close to 'in-tune', both LED's would be on?

> >It would be interesting to also pursue the generation of MIDI from a
> >guitar signal ....
>
> and from other real world events. In my free-time ( not much) I currently
> work on such a project : a microphone is placed at each end of a 1m plastic
> tube. If you hit the tube with e.g. a drum-stick the mikes generate pulses
> with a runtime delay. A 16C54 along with a 324 opamp digitizes these pulses
> ( MIDI key velocity ) and calculates a MIDI-note-# from the trigger delays.

This sounds like a really fun midi-trigger. I'd like to get a hold of
some of your results. Are you digitizing Mike output with RC discharge
time?

> I 'll put schematics, layouts + code related to MIDI, musicelectronics, PICs
> and other controllers on my ftp site as soon as possible ( late summer ?)
> Will be announced here and on MIDI.boards

I'll look forward to this.

> P.S.
> I too have some code that sends MIDI. MIDI IN is tough.
> a 20MHz PIC with HW-interrupt could do it. Somebody tried it ?

32 usecs/bit doesn't leave much time. Setting up an 'hc11 is quite
a bit easier.

> This has some possibilities. But you need to guess the reference freq.
> from early edge crossings to get the correct string. Help me out now,
> what would the algorithm look like in pseudo-code?
>
> wait for an edge crossing.
> start a timer.
> next edge crossing is 1/2 period.

Are you sure you can assume 50% duty cycle? Depending on the DC level
when you clip, the 1/2 cycle transitions will hopefully be near the
middle but you can't count on the exact timing. I time from one
low-to-high transition to the next to unask this question.

The rest of your algorithm looks good. What I do is wait for the edge,
then peek at the RTCC and look up the count in a table. Seehttp://www.rt66.com/dthomas/pic/pic.html if you want to see my tuner so
far.

> David, here are a few suggestions for a solution to your problem with
> harmonics:
>
> 1) Use a dual speed PLL to initially lock onto the strong fundamental
> in 'fast' tracking mode and then automatically switch to 'slow' mode
> to stay locked onto the decaying fundamental (decrease capture
> range). The 'fast' speed would be optimized to lock onto the
> fundamental quickly and the 'slow' speed would be fast enough to
> track tuning variations but too slow to 'jump' over to a harmonic.
> The fundamental could then be measured by coupling the vco to the
> pic.
>
> 2) Use zero crossing detection. It has the unique property of allways
> digitizing the lowest frequency, even if its not the strongest.
> A well designed front end, and possibly threshold tracking to
> maintain sensitivity could be used to extend its range.
>
> 3) Use a switched capacitor filter with external clock input configured
> as a 'comb' filter. Lock on to the fundamental and feed it into the
> filter's clock, thereby attenuating its harmonics. Something
> creative would need to be done here to aquire the fundamental in the
> first place.
>
> 4) Use the pic to implement an FIR or IIR filter.
>
> 5) Use the pic to integrate its measurement of frequency and then use
> that to detect the rate-of-change. If the rate of change (slope)
> exceeds a pre-set value then we know the detection circuitry has
> switched from the fundamental to a harmonic. At that point in time
> divide the new frequency measurement by the fundamental to calculate
> the harmonic factor and use that factor in subsequent calculations.
> This idea would work best if the signal were processed first by a
> PLL to lock onto a single spectral component. In fact, you could use
> the lock detection circuitry of the PLL to detect when the
> switchover to a harminic occurs.
>
> 6) Can you tell if a note is sharp or flat from its harmonic as well as
> from its fundamental? If so, then use a PLL front end to purify the
> spectrum (one frequency at a time) and do your function on the VCO
> signal.
>
> 7) Use an analog input and implement a PLL in pic firmware, using a
> combination of the above ideas.
>
> I really know absolutely nothing about music, but I hope these ideas are
> usefull, or stimulate the formulation of new ones.
>
> Regards, Dana Frank Raymond - Foxtrot Systems Ltd.
> Internet: dana.raymondspam_OUTcanrem.com. Compuserve: 73362,3052

Please help my confusion - it seems to me that we are trying to over engineer
what could be an easy solution.

Wouldn't the normal tuning process be:
1. "Pluck" the string.
2. Listen for it being sharp or flat.
3. Tune the string.
4. Re-pluck the string.

If this is the case, why worry about decaying harmonics? Just count
the number of zero crossings in 100 msec & compair to a table of expected
values. If it is high or low out-of-range discard it & try again.

Even if you tune on the fly ( i.e. pluck-listen-tune as a single process); I
would think you could still get a good frequency sample after 2 sec. I could
be wrong here - I've never tried it.

I don't know much about guitars. But I think the difference between the
1st string (low E) and the 6th (high E) is 2 octaves. With a string way out-
of-tune, I think it is possible to tune to the frequency of another string.
Taking a string this far out-of-tune would be undetectable by your circuit.
You may want to consider using 8 LEDs: 1 per string, plus a high and low
indication. This would let you know which string the circuit thought you
were tuning.

> Please help my confusion - it seems to me that we are trying to over engineer
> what could be an easy solution.
>
> Wouldn't the normal tuning process be:
> 1. "Pluck" the string.
> 2. Listen for it being sharp or flat.
> 3. Tune the string.
> 4. Re-pluck the string.

No. The way guitar players generally tune is to hit the string once,
good and hard, then adjust the peg as the string is decaying. That
way you can adjust a continuous pitch until it's right. If you try to
tune with only "that was too high" type information, it takes way too
many tries. Being off by even a few cents is enough to make the
instrument sound sour.

> Even if you tune on the fly ( i.e. pluck-listen-tune as a single process); I
> would think you could still get a good frequency sample after 2 sec. I could
> be wrong here - I've never tried it.

2 sec. is enough time to know if you're right or not, but it's not
enough time to bring the string in.

> I don't know much about guitars. But I think the difference between the
> 1st string (low E) and the 6th (high E) is 2 octaves.

<nod>

> With a string way out-
> of-tune, I think it is possible to tune to the frequency of another string.

Guitarists are smarter than this. Very few guitar players have any
trouble at all getting it close, even very close, by ear. The tuner
is just used for fine tuning. Also, guitars tend to drift some but
not a lot. A guitar that was packed up a month ago is unlikely to be
so far out of tune that an autodetect mechanism would identify the
wrong string. And most musicians wouldn't fall for it even then.

Still, your idea of providing a display that shows which string was
detected is a good one, provided you can afford to add it in your
particular application.

-> > 1) Use a dual speed PLL to initially lock onto the strong
-> Would it really stay locked to the weak fundamental?

Yes, I believe so. A PLL CAN track a signal outside its capture range if
it has been locked onto it in the first place and its loop
characteristic are such that it resists change that is too fast (a
change from the fundamental to a harmonic can be considered a fast
change). When the harmonic comes into the capture range (close enough in
frequency and strong enough compared to the fundamental) the PLL won't
move to it because its loop characteristics inhibit fast change.

-> It can't be that easy, can it? I'm already amplifying and heavily

No, you're right, I goofed. I'm still trying to find the reference in
"The Art of Electronics" that got me confused on that one (An excellent
reference BTW. I highly recommend it).

-> > 4) Use the pic to implement an FIR or IIR filter.
->
-> What are these?

Digital filters. You need a very fast CPU with an analog front end.

-> How does one "purify a spectrum" with a PLL? What does that mean?
-> But yes of course it is possible to track changes in a harmonic as
-> readily as the fundamental.

What I mean by that is that A PLL will lock onto one spectral component
(frequency) if it is coherent, stays within the tracking range, and
doesn't change too fast (slew rate). If you take a standard audio range
PLL and pluck a guitar string the PLL's oscillator will be one
frequency; The base note (terminology correct?), and the phase should be
shifted at around 90 degrees from the fundamental. The PLL will either
stay locked onto the fundamental or switch abruptly to a harmonic if
that harmonic is strong enough.

By measureing the PLL's oscillator frequency, you arn't getting a lot of
noise due to harmonics. Its only one frequency, changing over time.

> Yes, I believe so. A PLL CAN track a signal outside its capture range if
> it has been locked onto it in the first place and its loop
> characteristic are such that it resists change that is too fast (a
> change from the fundamental to a harmonic can be considered a fast
> change).

Okay yeah this makes sense. I just need to make the loop fast enough to
lock onto the fundamental before the harmonic gets too strong (500 ms
response is plenty good enough) but slow enough that the almost-instant
switch to the harmonic doesn't -- scuse the pun -- phase it.

I'd really like to try this approach. What would be some audio PLLs to
use? Perhaps a 565? 567? Or are there more modern, better ones? I've
worked extensively with RF type PLLs (gotta whole drawer full of motorola
chips) but never worked with audio PLLs.

Incidentally, for anybody wanting to use a PIC to tune an RF circuit, I
highly recommend the MC145170 chip. It's cheap (about $8), readily
available (Hamilton Hallmark, Newark), and miraculous. Programmed with 3
wire interface. Reference modulus can be any number from 4(?) to 32767.
Programmable divider from 40 to 65535. Can be programmed to be
single-ended or dual-ended. Ref. output available. Sign of loop can be
inverted by programming. Can directly synthesize up to 160 mc. XTAL ref.
osc. on-board. 16 pin package. I've used these with PICs and they work
great.

> All one needs is an audible tone refference to tune the guitar. As the two
> tones get closer they cause a 'RING' which allows a semi skilled musician
> to tune-a-fish.

Yeah that "ring" is called beating. It's the difference between the two
frequencies. And you are right that lots of musicians use this technique
all the time and it works great.

However, a lot of musicians prefer to use electronic tuners, at least in
some situations. Particularly, stage musicians like to have them.
You're playing and a string breaks -- you finish the song, then hit a
foot pedal and suddenly your tuner (with visual indicator) is in circuit
and your amp is not. You tune up without bothering the audience, then
you're back in business. Or, let's say another band is playing, and your
band is up next. You probably couldn't hear yourself to tune, perhaps even
with a headset.

I personally am very accustomed to relying on my ears, a lot more than
electronic measuring devices. I've never tried to play an instrument on
stage that I didn't first check with my ears. But a lot of pro musicians
do this sort of thing all the time.

Guitarists pluck the string and adjust the tuning while it sounds, and I for
one would not consider it useful to have to stop the string and re-pluck
to check the adjusted frequency.

> If this is the case, why worry about decaying harmonics? Just count
> the number of zero crossings in 100 msec & compair to a table of expected
> values. If it is high or low out-of-range discard it & try again.

The upper harmonics do decay quite quickly. The first and second harmonic
can be a problem.

> Even if you tune on the fly ( i.e. pluck-listen-tune as a single process); I
> would think you could still get a good frequency sample after 2 sec. I could
> be wrong here - I've never tried it.

I normally don't get a useful tuning measurement out of my commercial tuner
until about 1/2 second after the string is plucked. If you can't get a
good frequency sample for 2-5 seconds/pluck, it is really laborious to
tune.

> I don't know much about guitars. But I think the difference between the
> 1st string (low E) and the 6th (high E) is 2 octaves. With a string way out-
> of-tune, I think it is possible to tune to the frequency of another string.
> Taking a string this far out-of-tune would be undetectable by your circuit.
> You may want to consider using 8 LEDs: 1 per string, plus a high and low
> indication. This would let you know which string the circuit thought you
> were tuning.
>
>
> Jon Poland
> Sun Microsystems Inc.
> St. Louis, MO
> (314) 569-4716
> RemoveMEjon.polandspam_OUTKILLspamcentral.sun.com

The two E strings are indeed 2 octaves apart. You can indeed tune a string
down to the next one, if you try. The harmonics on a given pluck on a string
won't let you simply count zero crossings, as you can pluck to generate
harmonics or not. The zero crossings should be harmonically related, though.

The low E is about 82.41 Hz, the A is about 110.0 Hz, the high B is
247.2 Hz, high E is 329.6 Hz. Referenced to A-440Hz. It's very useful
to be able to adjust the reference of a tuner. Acoustic pianos are out
most of the time in most places.