DSPRelated.com
Forums

simple sampling algo question

Started by danielsan March 4, 2006
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.
Hello Daniel,


> 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.
If you don't have a priori knowledge of the frequency a DFT isn't going to help. All it would tell you is that a certain frequency is there or isn't there. You would need do do an FFT since the DFT only looks at one single frequency, plus or minus half the bin width. However, FFT in this case sounds like squashing a fly with a half-ton boulder. Why not just feed that signal into the timer input of a uC (or DSP) and read and reset the timer count at regular intervals? For your desired precision these read intervals should be longer than a few hundred msec. That is the same way a frequency counter usually works unless it's switched to period measurement. Regards, Joerg http://www.analogconsultants.com
Joerg wrote:
> Hello Daniel, > > >> 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. > > > > If you don't have a priori knowledge of the frequency a DFT isn't going > to help. All it would tell you is that a certain frequency is there or > isn't there. You would need do do an FFT since the DFT only looks at one > single frequency, plus or minus half the bin width. > > However, FFT in this case sounds like squashing a fly with a half-ton > boulder. Why not just feed that signal into the timer input of a uC (or > DSP) and read and reset the timer count at regular intervals? For your > desired precision these read intervals should be longer than a few > hundred msec. That is the same way a frequency counter usually works > unless it's switched to period measurement.
Joerg, I think you have a case of temporary acronym confusion. An FFT /is/ an efficiently coded DFT. Were you thinking Goertzel, by chance? Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
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? 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. For higher rates of observation, you might want to try some form of period measurement, such as autocorrelation, perhaps averaged over several periods. For measuring a single frequency at intermediate rates with no nearby interfering signals, one trick I would try would be to use an adaptive single-bin phase vocoder on partially overlapped sample windows, using the Goertzel algorithm to measure phase differences. This seems to have both good accuracy and low computational overhead. IMHO. YMMV. -- rhn A.T nicholson d.0.t C-o-M
Joerg 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. > > > If you don't have a priori knowledge of the frequency a DFT isn't going > to help. All it would tell you is that a certain frequency is there or > isn't there. You would need do do an FFT since the DFT only looks at one > single frequency, plus or minus half the bin width.
If you know there is only one frequency of interest in a narrow enough band with little interference, then a full FFT may be overkill. The calculation of only a few DFT bins may suffice, either using interpolation, successive approximation, or (my favorite trick these days) using the phase differences between the same bin in successive frames (similar to phase vocoder analysis). If you adaptively track the single-bin frequency, and the frequency changes slowly enough, you may be able to get by with as little as a single DFT bin calculation per analysis frame (N multiplies). So a single-bin DFT calculation might help after all. IMHO. YMMV. -- rhn A.T nicholson d.0.t C-0-M
Ron N. 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? > > 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. > > For higher rates of observation, you might want to try some > form of period measurement, such as autocorrelation, perhaps > averaged over several periods. > > For measuring a single frequency at intermediate rates with no > nearby interfering signals, one trick I would try would be to use > an adaptive single-bin phase vocoder on partially overlapped > sample windows, using the Goertzel algorithm to measure > phase differences. This seems to have both good accuracy > and low computational overhead.
The way we did this was to count the pulses in a period suitable for the frequency range. when the period elapses, the count is transferred to an output latch, and the counter restarted from zero. Changing the period calibrates the sensor in any desired units. You can have a direct readout with a decimal counter and a numeric display. You can use a binary counter and average a few readings to smooth variations by using a computer. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
>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?
>For higher rates of observation, you might want to try some >form of period measurement, such as autocorrelation, perhaps >averaged over several periods. >
I'm just searching for the simplest answer. That's why I figured the FFT/DFT route would be the easiest. We don't mind if the calculation takes a couple of seconds. We can only sample for a quarter of a second of so @ 2-4x oversampling with the memory limitation.
>For measuring a single frequency at intermediate rates with no >nearby interfering signals, one trick I would try would be to use >an adaptive single-bin phase vocoder on partially overlapped >sample windows, using the Goertzel algorithm to measure >phase differences. This seems to have both good accuracy >and low computational overhead. >
Tell me more? As I said I'm interested in the solution, and a little elegance never hurts. -Tom
> >IMHO. YMMV. >-- >rhn A.T nicholson d.0.t C-o-M > >
>Ron N. 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? >> >> 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. >> >> For higher rates of observation, you might want to try some >> form of period measurement, such as autocorrelation, perhaps >> averaged over several periods. >> >> For measuring a single frequency at intermediate rates with no >> nearby interfering signals, one trick I would try would be to use >> an adaptive single-bin phase vocoder on partially overlapped >> sample windows, using the Goertzel algorithm to measure >> phase differences. This seems to have both good accuracy >> and low computational overhead. > >The way we did this was to count the pulses in a period suitable for the
>frequency range. when the period elapses, the count is transferred to an
>output latch, and the counter restarted from zero. Changing the period >calibrates the sensor in any desired units. You can have a direct >readout with a decimal counter and a numeric display. You can use a >binary counter and average a few readings to smooth variations by using >a computer. > >Jerry >--
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? -TOm
danielsan wrote:
>>Ron N. 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? >>> >>>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. >>> >>>For higher rates of observation, you might want to try some >>>form of period measurement, such as autocorrelation, perhaps >>>averaged over several periods. >>> >>>For measuring a single frequency at intermediate rates with no >>>nearby interfering signals, one trick I would try would be to use >>>an adaptive single-bin phase vocoder on partially overlapped >>>sample windows, using the Goertzel algorithm to measure >>>phase differences. This seems to have both good accuracy >>>and low computational overhead. >> >>The way we did this was to count the pulses in a period suitable for the > > >>frequency range. when the period elapses, the count is transferred to an > > >>output latch, and the counter restarted from zero. Changing the period >>calibrates the sensor in any desired units. You can have a direct >>readout with a decimal counter and a numeric display. You can use a >>binary counter and average a few readings to smooth variations by using >>a computer. >> >>Jerry >>-- > > > 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. Whether the sensor counter is zeroed after each reading or used like a capture register depends on your �controller's architecture. by making the count period longer or shorter You can change the reading at full scale by making the counting interval longer or shorter. It is OK to wrap around once (but only once) with a capture register. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
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.
> >IMHO. YMMV. > >-- > >rhn A.T nicholson d.0.t C-o-M > > > >