If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Registered Member #72
Joined: Thu Feb 09 2006, 08:29AM
Location: UK St. Albans
Posts: 1659
Rather than hijack or get lost on the "Frequency counting without a microcontroller" thread ...
Has anyone considered frequency counting with a PC, using the sound card input? The general idea is that if the desired signal is in the audio range, then it's just put in directly. If it is an RF or microwave signal, then you need to use a fixed prescaler to knock the frequency down to below 15kHz, which is about as cheap and simple as a counter front-end gets.
If the required accuracy is low, then measuring a known signal one time to estimate the sound card's timebase frequency will be good to better than 100ppm, maybe even 10ppm depending on the stability of the sound card. If you want higher accuracy, then you can measure a suitable reference on one channel and the signal on the other, and compare the two directly.
Obviously the resolution/accuracy you can acheive depends on the algorithm that you use to interpret the signals. I've come up with an algorithm that performs close to the noise limit of 1 part in 65k.N, where N is the number of samples in the record, and 65k is the nominal full scale of the ADC. A record of 1024 samples would then get a resolution of the order of 15ppb. This sort of resolution translates to a few Hz at microwave frequencies, even through a fixed prescaler.
Obviously use of a prescaler gives lower resolution than counting the IF that would come out of a mixer. However, a prescaler can be had for a few $ and gives performance adequate for many applications. A down-converter to IF is a whole different ball game.
I devised this algorithm to track minute changes in a capacitive transducer built into an oscillator, for seismic measurements, but the code has been useful at work as well for measureing the settling time of a fast synthesiser using an ADC evaluation capture card.
Before I spill the beans on my algorithm (I will), I am interested to know if anybody has used a PC like this, and how they might go about a high resolution frequency measurement algorithm.
Registered Member #27
Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
I know several people on the forum has used PC sound cards like that. I have even managed to count frequencies up in the MHz range using the printerport.
PCs are getting less and less useful in this area so I am more interested in if the algorithm is easy to implement using fixed point math to run on a microcontroller.
Registered Member #30
Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
I've used Spectrum Lab with a pro soundcard and a Tayloe I/Q downconverter before. I don't think it does frequency counting as such, it just FFTs a whole 96kHz wide slice of spectrum. You can move a cursor around and get a readout of the frequency, but I think that only has a resolution of one FFT bin.
It can do all sorts of other cool stuff like demodulating AM, FM and SSB in software, though.
Registered Member #1232
Joined: Wed Jan 16 2008, 10:53PM
Location: Doon tha Toon!
Posts: 881
Hi Neil.
I have used the PC sound card "line in" to record and process many signals over the years. Using it as a poor-mans data aquisition card for AC content I guess. And in fact I regularly use Goldwave on the PC at work to show me an FFT of a slice of spectrum around 5MHz in realtime by first mixing it down to baseband.
The only problem I have seen with this approach is that the sample frequency that the soundcard actually gives you is sometimes not exactly what you ask for! Old Creative Labs SB16 cards are really bad for this because the sample rate is divided down from a 1MHz crystal. Integer divisions give quite bad errors particularly at high sample rates. They aren't always immediately apparent if audio is recorded and played back on the same soundcard, at the same "wrong" rate. I have also found that it can drift slightly, at least as the Dell PC here on my desk warms up.
Apparently most modern soundcards sample at 48kHz and then use fancy multi-rate DSP algorithms to resample and decimate to whatever rate you tell them you want audio data at. I'd guess that sample rate errors get introduced due to truncation or rounding errors in this resampling process.
Whenever I acquire anything important that I want to measure the frequency of accurately, I always use the full 48kHz sample rate, and I will set up a 1kHz reference first on an Agilent 33250A generator here at work. I sample that first then the wanted signal.
I've used various ways to determine the frequency including:
1. Doing an FFT of the sampled data in Matlab, (or as a last resort Excel.) 2. Measuring the period of the waveform (or several periods) and calculating the frequency manually 3. Counting cycles over a given number of samples and calculating the frequency manually 4. Beating the recorded signal with a computed reference signal (modulation) to find the exact frequency
Method 1 is good for noisy signals or where there are multiple frequency components. Method 2 is good for low frequency signals because you can get an accurate measure of the period in samples. Method 3 is good for high frequency signals where their period is not an integer number of audio samples. Method 4 is good if you want to find a high frequency to a lot of decimal places and have a long record length.
Registered Member #72
Joined: Thu Feb 09 2006, 08:29AM
Location: UK St. Albans
Posts: 1659
The algorithm measures the frequency by differencing the phase over some interval. The trick is getting a low noise estimate for the phase at the ends of the interval. The performance varies depending on the form of the noise on the sampled signal, but the following is close to optimum if there are no strong spurious components.
I'm not going to give code, it's written in Matlab, and not that comprehensible, it's better to describe it.
Suppose I'm measuring the average frequency over some sample interval. First estimate the frequency over the interval. One way is to take an FFT of the windowed samples in the interval. Take the centre of gravity of the power peak (the first moment of the power spectrum around the peak divided by the zeroth moment), this gives me a good first estimate of the frequency (which for some low accuracy applications is good enough as it stands). Another, more suitable for fixed point, is to estimate zero crossings at the end points, then count cycles between them. In either case, the estimate must be accurate enough, no more than 10% of a sample time wrong in the length of the interval, to build the following filter.
Use this frequency estimate to construct a complex filter, windowing a sine and cosine, at the frequency estimate. It is convenient and quite good performance if the filter is the same length as the interval, this simplifies indexing into the data, though on pure resolution grounds a filter that's longer than the interval is better. How much longer depends on the window, higher order windows can use respectively longer filters for best performance as their time domain tails fall faster. I only use either Gaussian or Blackman-Harris windows, both families are good performance, easy to compute, and easy to tune for resolution/sidelobes. 3rd order BH is a good place to start, though other windows may give better performance with specific types of noise.
Convolve the filter twice with the sample waveform, once centred at each end point of the interval. The windowing reduces the noise on the signal, the length of the filter gives high resolution. This gives the complex signal at each end point, from which a call to ATAN2 gives the phase. The difference between the two phases is the frequency; however, the phase difference is ambiguous to a multiple of 2pi. Now use the first estimate of the frequency to supply the number of whole cycles. Divide the phase difference by time and the answer is frequency.
For use in fixed point, the method is already combatting the quantisation noise on the sampled signal itself, so will be resistant to the quantisation effects of short words representing the filter coefficients. The convolutions will work fine as long as the products can be accumulated in a much longer word (most DSP and uC that I've seen allows double precision product accumulation). Basically, if you want to work for a particular frequency resolution, then the convolution results must have a higher resolution than that.
Bear in mind that this method measures the average frequency over the interval. If the signal is modulated, including by phase noise, modulation with frequencies higher than 1/interval will be aliased. This means that for the highest performance, the interval should be short, and the result low pass filtered from a sequence of frequency estimates. This is essential if camparing a signal on one channel with a high quality reference on the other channel, when the sound card has a dodgy drifty clock.
Registered Member #1232
Joined: Wed Jan 16 2008, 10:53PM
Location: Doon tha Toon!
Posts: 881
Your method seems to make sense to me. If I understood it correctly, you do a short-FFT to get an approximate frequency of the strongest component present. Then you construct a matched bandpass filter to act on the original signal and remove most of the noise away from the frequency range of interest. Finally you examine the starting and ending phase of the filtered signal to get the fractional period information and improve on the initial frequency estimate by the FFT? Pheeewwwww, Did I get that right?
I wasn't sure that you could extract any more information out of the original sample data than a single long FFT would provide, but I guess you can, or else you wouldn't have developed this algorithm? It is clear to me from your explanation that you know more about this DSP stuff than me.
I'm familiar with a few pitch-detection algorithms, which use autocorrelation or Average Magnitude Difference Function (AMDF) or Average Squared Difference Function (ASDF) but I have never previously seen the method you have described here. Have you tried it with various test signals to see how accurately it measures the frequency?
The only bit I would question is this part:
"Convolve the filter twice with the sample waveform, once centred at each end point of the interval." If the FIR filter is centred on the first and last samples of the recorded section then half of the impulse response of each FIR filter will be lost despite the windowing? Or do you mean the you definte an interval as some subsection of the full recorded data? So data exists before the start point of the interval, and after the end point?
Interesting stuff!
-Richie,
Edit: It might be worth comparing what you are doing with what the guys here are doing if you're not already aware of their tricks...
Registered Member #72
Joined: Thu Feb 09 2006, 08:29AM
Location: UK St. Albans
Posts: 1659
I knew I should post a diagram
Within a long time record, I'm measuring the mean frequency only between points 1 and 2 on the time/sample_number axis. The Y axis shows the magnitude envelope of the filters, or of the window used to multiply the sine/cos. In this diagram, the filters are 30% longer than the measurement interval, so overlap in the middle. It may seem counter-inutuitive to include the same middle data twice, but for "good" windows, the amplitude at this point will be very small, and indeed the purpose of including data far from points 1 and 2 is to reduce the noise due to aliasing of the phase estimates. And indeed it works.
This is not really a tone estimation algorithm, which has to be robust in the presecence of multiple tones, harominics and subharmonics, and deliver a reasonable measurement. It's really to extract the maximum possible resolution over a restricted time interval for a single tone. However, it does make a pretty good job of measuring whichever frequency is chosen to construct the filters, strongest or not, as long as multiple tones are fully resolvable by the window spectrum over the interval length.
My test cases have mainly been synthetic signals with added noise, to test the resolution of the algorithm itself, any real digitised signal is degraded by the sound card and the signal source, though it is certainly worth playing around with interval lengths and window types to optimise for the particular card. One test case I have used is a real single signal, phase-shifted differently into the two channels. The comparison noise was consistent with the few LSBs of noise that the ADC added.
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.