Maker Pro
Maker Pro

Using a PIC's A/D for a 'spectrum analyzer'

J

Jeremy D. Grotte

Jan 1, 1970
0
I had a bit of a 'brain-storm' while I was thinking about how to extract
frequency component information from an analog audio signal. I'll be making
a bunch of assumptions here, and throw a bunch of numbers which may/may not
be usable/believable/correct, but I'm trying to work out the theory, so
please bare with me...

Ok, say you take a PIC's A/D converter, hook it up to a low volt analog
input, say from a headphone jack or an input from an RCA connector. You do
the A/D conversion at say 1khz (keeping everything in 10's so the math is
easier), I'll also assume (for easyness) that the range of the A/D's output
is 0-99 (50 being midrange) and that's all the input signal will do anyways.

Ok, in a nutshell, if the previous reading was a 0, and the current reading
(1ms later) is a 99, then that would mean that the input signal is a 500hz
signal, which would also presume that the next reading would be a 0. So
now, I can deduce that I have a very strong 500hz signal at the input. Ok,
extrapolating that theory, if the previous reading was a 0, and the current
reading is a 50 and the next reading would be a 99, then the input signal
would be 250hz...and so on and so on...

Therefore, I could store these in a FIFO buffer, and continuously do the
math, going back farther in the buffer, looking at the signal level vs. time
and calculate signal strengths and frequencies from this.

Ok, so I got that...Problem is...a 500hz signal could, in theory, look
exactly like a 250hz signal, which could also look exactly like a 125hz
signal, and so on... And of course, all of these numbers I'm throwing out
are theoretical and don't mean anything, just easy numbers to work with.

How would a person compensate for this, in software?

Is this something new (probably not), or is this something that's been going
on for years and years and I have missed something completely?

Basically, where this is going is a project to run a bunch of LED's which
will respond to different audio freq's, pulse the blue for low freq's, pulse
the green for mid-freq's, etc.etc.etc. But I don't want a bunch of
hi/low/band pass filters running around...too many pieces/parts. It's
probably easier in software anyways...

JDG
 
R

Rene Tschaggelar

Jan 1, 1970
0
Jeremy said:
I had a bit of a 'brain-storm' while I was thinking about how to extract
frequency component information from an analog audio signal. I'll be making
a bunch of assumptions here, and throw a bunch of numbers which may/may not
be usable/believable/correct, but I'm trying to work out the theory, so
please bare with me...

Ok, say you take a PIC's A/D converter, hook it up to a low volt analog
input, say from a headphone jack or an input from an RCA connector. You do
the A/D conversion at say 1khz (keeping everything in 10's so the math is
easier), I'll also assume (for easyness) that the range of the A/D's output
is 0-99 (50 being midrange) and that's all the input signal will do anyways.

Ok, in a nutshell, if the previous reading was a 0, and the current reading
(1ms later) is a 99, then that would mean that the input signal is a 500hz
signal, which would also presume that the next reading would be a 0. So
now, I can deduce that I have a very strong 500hz signal at the input. Ok,
extrapolating that theory, if the previous reading was a 0, and the current
reading is a 50 and the next reading would be a 99, then the input signal
would be 250hz...and so on and so on...

Therefore, I could store these in a FIFO buffer, and continuously do the
math, going back farther in the buffer, looking at the signal level vs. time
and calculate signal strengths and frequencies from this.

Ok, so I got that...Problem is...a 500hz signal could, in theory, look
exactly like a 250hz signal, which could also look exactly like a 125hz
signal, and so on... And of course, all of these numbers I'm throwing out
are theoretical and don't mean anything, just easy numbers to work with.

How would a person compensate for this, in software?

Is this something new (probably not), or is this something that's been going
on for years and years and I have missed something completely?

Basically, where this is going is a project to run a bunch of LED's which
will respond to different audio freq's, pulse the blue for low freq's, pulse
the green for mid-freq's, etc.etc.etc. But I don't want a bunch of
hi/low/band pass filters running around...too many pieces/parts. It's
probably easier in software anyways...


Ahem,
to start with you need some storage for an FFT.
Lets assume 8 bit data, giving approx 20dB range (yawn).
When sampling with 1ksample the upper frequency is 500Hz.
The lowest frequency also makes the resolution.
So a resolution of 1Hz requires sampling over 1 second, approx.
Say the lower frequency is 10Hz that means you need 100 samples.
FFT need power of 2, so make it 128 bytes. And another 128 bytes
for the output. So every 100ms you can compute a 128point FFT that
takes(how long ?) 1 second ? 10 seconds ?



Rene
 
J

John Jardine

Jan 1, 1970
0
[clip]
Other than an FFT, you surely should be able to measure the 'average sample
to sample, rate of change' of a block of say 20 samples. The higher the
number, the higher the frequency.
High frequency, high voltage gives a max result.
High frequency, low voltage gives a ?? result
Low frequency, high voltage gives a ?? result
Low frequency, low voltage gives lowest result.

No?

regards
john
 
O

onestone

Jan 1, 1970
0
Hi Rich, I was actually thinking of Numerical Recipes in C. I realised
after I'd posted, it's reasonably straightforward, but not for beginners
I guess.

Al

Rich said:
[snip...snip...]
There used
to be an excellent DSP primer on the net that could be downloaded by
chapters, but I've no idea where you might find it.

http://www.dspguide.com/


it's all C IIRC


FORTRAN! Pretty straightforward to recode, though.

and
includes samples of IIR filters. I don't recall if it includes filter
calculations.


Some. Enough to get started with and enough to be able to understand
what's going on when punching tables (or software) for other filters.

The OP should definitely get the Smith book above. Signal processing can
actually be kind of fun and the book is a great intro (esp. for free).

Also grab a copy of Scilab 2.7 from http://www-rocq.inria.fr/scilab/
(which seems to be down at the moment?). It's a free tool that's similar
to Matlab and includes canned routines for filter design.
 
O

onestone

Jan 1, 1970
0
Jeremy said:
Fourier Transforms...
I knew I'd heard that somewhere before, didn't know what it was all about.
I've downloaded a bunch of pdf's to read thru all about FFT.
Thanks for all the information from everybody. I knew I couldn't possibly
be the first person ever to think of such a thing.
Now to just figure out how much I could actually do with a 40mhz PIC on 10
bit A/D data with limited ram space. My goal isn't a perfect real time
audio spectrum analyzer, just a digital 'filter' of sorts to flash a bunch
of LED's according to the levels of frequencies coming across the wires.
JDG

In that case Jeremy look at using IIR bandpass filters. They require
less storage than FIR or FFT, fewer coefficents, and you could probably
get it working real time. use 4 pole filters. Each filter requires 3 x
16 bit coefficients (8 bits will not work properly) and 3 x 16 bit
variables. Even a lower end PIC can handle that.

Al
 
J

Jonathan Kirwan

Jan 1, 1970
0
In that case Jeremy look at using IIR bandpass filters. They require
less storage than FIR or FFT, fewer coefficents, and you could probably
get it working real time. use 4 pole filters. Each filter requires 3 x
16 bit coefficients (8 bits will not work properly) and 3 x 16 bit
variables. Even a lower end PIC can handle that.

I think that is a better approach, computationally. It appears
Jeremy is thinking of something with a few fixed, wide bands to
examine. Using appropriately designed biquads, for example,
would seem to make some better sense than ffts.

By the way, for Jeremy, there's a *lot* of material on FFTs and
signal processing. But I can personally recommend some books
for someone getting started into these areas. Nicholas John
Loy's book, An Engineer's Guide to FIR Digital Filters, is a
very intuitive, not too mathy approach to understanding some
applications of FIR filters (and the FFT, as well.) [Of course,
there is one of the several "Numerical Recipes" books, which
have already been mentioned.] For the FFT itself, E Oran
Brigham writes The Fast Fourier Transform and I found that book
(I worked out of the 1974 first edition, there may be later
editions but I can't comment on them) to be stellar in trying to
understand the FFT in detail. It's mathy, but the math is very
well organized and new ideas are logically laid upon the earlier
foundations in a clear fashion. More, it is so well
supplemented by parallel examples and pictures that even without
the math you'll get an excellent understanding from it, I
believe. For a variety of algorithms and example code to try
your hand at, there is Paul M Embree and Bruce Kimble's C
Language Algorithms for Digital Signal Processing. If you read
carefully through their work, you'll learn a lot. It's not just
code, there are some excellent bits of information in there.

Jon
 
Top