DSPRelated.com
Forums

simple sampling algo question

Started by danielsan March 4, 2006
danielsan wrote:
> Very simple task. We have a sensor that puts out a frequency proportional > signal to its input. We have a freq2volt converter but would like to take > it out, and just sample the waveform to extract the frequency. The range > is from 5KHz to 8KHz and we'd like to have accuracy in the range of 6-12Hz > sampling at 8bits accuracy. Probably 24KHz should do it. Can someone just > go over the basic algorithm for a non optimized DFT? It's all a little > hazy from my DSP class 5 years ago.
Many methods are possible, but here's one that's pretty simple to implement: mix with a complex sinusoid (that is, a cosine and sin) whose frequency is the center of your band, 6.5 kHz. This centers your signal on zero Hz, popularly known as "baseband". The mixing becomes particularly simple if you mix with Fs/4, because Fs/4 only has 4 different values, two of which are 1 and 0, and the other two of which are +/- sqrt(2)/2. As it turns out, 4 * 6.5 kHz = 26 kHz, which ain't far from 24 kHz. (O happy day! as Eric Jacobsen once said here many years ago, IIRC.) Given that you've mixed down to baseband, you can determine the phase difference between successive samples via a complex multiply of the current sample with the conjugate of the previous sample. Next, take the arctangent of the result, scale it by Fs (as well sundry constants left as an exercise for the reader, not me ;-), and, _voila_!, you have a stream of instantaneous frequency values. Next, it might not be a bad idea to filter or average those in some way, if you can't be absolutely certain that your input signal truly was a sinusoid (and let's face it--who can be when you go around using an 8-bit converter? ;-) Coincidently, that's pretty-much the way I used to implement FM demodulators years ago when I was in the business of implementing FM demodulators. (O happy day!) =g2 _____________________________________________________________________ Grant R. Griffin Publisher of dspGuru http://www.dspguru.com Iowegian International Corporation http://www.iowegian.com See http://www.iowegian.com/img/contact.gif for e-mail address
Ron N. wrote:
> danielsan wrote: > >>>danielsan wrote: >>> >>>>Very simple task. We have a sensor that puts out a frequency >> >>proportional >> >>>>signal to its input. We have a freq2volt converter but would like to >> >>take >> >>>>it out, and just sample the waveform to extract the frequency. The >> >>range >> >>>>is from 5KHz to 8KHz and we'd like to have accuracy in the range of >> >>6-12Hz >> >>>>sampling at 8bits accuracy. >>> >>>How rapidly can the signal change, or how rapidly to you need to >>>update the sensor reading? >>> >> >>Not too fast. It's a capacitive humidity sensor, so we can certainly >>optimize that once the frequency is known we only do nearby bins. Actually >>processing time is not an issue at all, and probably it won't change much >>more than a few hundred hertz tops since it's controlling a feedback >>loop. >> >> >>>If the rates are slow enough, you could just use a 1-bit A/D >>>(e.g. a comparator) to count zero crossings every quarter to >>>half second or so, perhaps after an analog bandpass filter to >>>remove some noise. >> >>The rates are slow. What would be the procedure here? > > > Dirt simple. Count transistions from negative to positive (or > from below the bias center voltage to above) for one second. > In the absence of enough noise to cause false transitions, that > count value is your frequency +-1. For a lower accuracy, > but faster updates, count for 1/Nth of a second and multiply > the count by N (for +-N accuracy). A simple analog (RC) > low pass filter might be all that is needed to ensure that > there are likely not enough false transistions to spoil your > reading. > > DFT/FFT type procedures might be needed only if you have > to extract your frequency measurement out of some noise, > or if you need to make sure a valid signal within the right > frequency range is there in the first place. > > What sort of latency will your feedback loop safely allow? > > A fancier interpolation method might allow measuring the > frequency using a smaller time window of data, and thus, > if the computational overhead in time doesn't increase too > much in exchange, could provide lower latency.
With a clean enough signal and sufficiently fine-grained time resolution, you can measure the period of a sensor signal. The reciprocal is the frequency, but for control over a narrow enough range, the nonlinearity caused by not dividing is acceptable in a control loop. For a target of period of T and an actual period of T + dT, T/(T + dT) = (f + df)/f ~= (T - dT)/T = 1 - dT/T. It is usually possible to arrange that T be a power of 2. Even if not, its reciprocal can be calculated once, then used as a multiplier. Very fast updated are possible this way. Without sampling, there is no need for an anti-alias filter. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Grant Griffin wrote:
> danielsan wrote: > > Very simple task. We have a sensor that puts out a frequency proportional > > signal to its input. We have a freq2volt converter but would like to take > > it out, and just sample the waveform to extract the frequency. The range > > is from 5KHz to 8KHz and we'd like to have accuracy in the range of 6-12Hz > > sampling at 8bits accuracy. ... > > Many methods are possible, but here's one that's pretty simple to > implement: mix with a complex sinusoid (that is, a cosine and sin) whose > frequency is the center of your band, 6.5 kHz. This centers your signal > on zero Hz, popularly known as "baseband". > > The mixing becomes particularly simple if you mix with Fs/4, because > Fs/4 only has 4 different values, two of which are 1 and 0, and the > other two of which are +/- sqrt(2)/2. As it turns out, 4 * 6.5 kHz = 26 > kHz, which ain't far from 24 kHz. (O happy day! as Eric Jacobsen once > said here many years ago, IIRC.) > > Given that you've mixed down to baseband, you can determine the phase > difference between successive samples via a complex multiply of the > current sample with the conjugate of the previous sample. Next, take > the arctangent of the result, scale it by Fs (as well sundry constants > left as an exercise for the reader, not me ;-), and, _voila_!, you have > a stream of instantaneous frequency values. Next, it might not be a bad > idea to filter or average those in some way, if you can't be absolutely > certain that your input signal truly was a sinusoid (and let's face > it--who can be when you go around using an 8-bit converter? ;-)
Very interesting. This appears similar to a 1-bin phase vocoder algorithm, except taken to the limit of calculating the phase information every sample instead of every DFT window offset. The advantage seems to be in getting an instantaneous frequency every sample, instead of at the DFT window offset rate. However the cost is in computing an arctan every sample. A phase vocoder requires computing an arctan only once per window offset, which can be far less often (depending on the maximum rate of change of frequency and the desired result rate, of course). Averaging the instantaneous phase/frequency values could be similar to the averaging inherent in summing up one bin of a DFT. IMHO. YMMV. -- rhn A.T nicholson d.0.t C-o-M
"Jerry Avins" <jya@ieee.org> wrote in message 
news:_audnRBnlOfix5fZRVn-tg@rcn.net...

>> This sounds familiar. I think I used something similar >> with interrupt >> driven code to calculate a frequency. We've already got >> the sensor >> integrated with a uController ADC line. Can you elaborate >> on the relation >> between counts and frequnecy/period relating to sampling >> frequency? > > You don't sample. You let a counter run for a fixed > interval, accumulating the sensor cycles. You will > probably determine the interval with another counter > counting down the processor clock.
In this case, your approach is probably the right one. In applications where the pulse rate is low (e.g.: some kinds of flow meters, watt-meters, etc.), a better approach is to measure period. For this you need a fixed-frequency clock, and you count its pulses between cycles of the unknown frequency. Then invert the period to calculate the frequency. If your clock counter overflows, you might decide that the frequency is zero.
Hello Jerry,

> > I think you have a case of temporary acronym confusion. An FFT /is/ an > efficiently coded DFT. Were you thinking Goertzel, by chance? >
Yes, probably so. I was thinking of single-bin DFT, Goertzel and all that. I still think this job just requires a timer, no fancy math. Unless, of course, all the timers are used up already but even then FFT seems like overkill. Regards, Joerg http://www.analogconsultants.com
Joerg wrote:

> Hello Jerry, > >> >> I think you have a case of temporary acronym confusion. An FFT /is/ an >> efficiently coded DFT. Were you thinking Goertzel, by chance? >> > > Yes, probably so. I was thinking of single-bin DFT, Goertzel and all that. > > I still think this job just requires a timer, no fancy math. Unless, of > course, all the timers are used up already but even then FFT seems like > overkill. >
Trying to do this with an FFT is probably like trying to drive screws with a sledgehammer -- it's overkill _and_ won't work as well as the best method. If the input signal is very clean then a timer that counts up for a 0.2 second window will give the desired rep rate _and_ will have an accuracy in excess of 10 bits. This requires two timers, though, and PIC's aren't always long on timer resources. Furthermore IIRC they won't do a gated timer of the sort you need for this job. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/
Hello Tim,


> If the input signal is very clean then a timer that counts up for a 0.2 > second window will give the desired rep rate _and_ will have an accuracy > in excess of 10 bits. This requires two timers, though, and PIC's > aren't always long on timer resources. Furthermore IIRC they won't do a > gated timer of the sort you need for this job. >
Why two timers? Regards, Joerg http://www.analogconsultants.com
Joerg wrote:
> Hello Tim, > > >> If the input signal is very clean then a timer that counts up for a >> 0.2 second window will give the desired rep rate _and_ will have an >> accuracy in excess of 10 bits. This requires two timers, though, and >> PIC's aren't always long on timer resources. Furthermore IIRC they >> won't do a gated timer of the sort you need for this job. >> > > Why two timers? > > Regards, Joerg > > http://www.analogconsultants.com
One for the zero crossings, one for for the 0.2 seconds. Or you could count zero crossings in software (this was mentioned in another thread) -- given the frequency this is probably better and I should have thought of it. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/
Hello Tim,


> If the input signal is very clean then a timer that counts up for a 0.2 > second window will give the desired rep rate _and_ will have an accuracy > in excess of 10 bits. This requires two timers, though, and PIC's > aren't always long on timer resources. Furthermore IIRC they won't do a > gated timer of the sort you need for this job. >
Come to think of it, you are right. One timer to count, another to set the interval. Although, at 500msec the latter could be the watch dog timer. Possibly the count timer could be replaced with an ISR where a coded counter clocks up and only the interval is set by a "real" timer. 8kHz isn't too fast for a little assembler routine unless the uC is already quite loaded with other stuff. All the ISR would have to do is bump a 16bit register value by one and return. When the period is up this register is read out. Regards, Joerg http://www.analogconsultants.com
Joerg wrote:
> Hello Jerry, > >> >> I think you have a case of temporary acronym confusion. An FFT /is/ an >> efficiently coded DFT. Were you thinking Goertzel, by chance? >> > > Yes, probably so. I was thinking of single-bin DFT, Goertzel and all that. > > I still think this job just requires a timer, no fancy math. Unless, of > course, all the timers are used up already but even then FFT seems like > overkill.
I think we ans several others agree. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;