PaulStoffregen / FreqMeasure

Measures the elapsed time during each cycle of an input frequency.
http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
69 stars 31 forks source link

"discrete frequency"/ limited resolution issue #20

Closed 0i0i0i closed 2 years ago

0i0i0i commented 2 years ago

Description

I used a high quality signal generator to test how much resolution this lib can do and try to understand how the frequency is measured . I find a strange issue. I started at 1600Hz and went to 1600.001, 1600.002, 1600.003 all the way to 1600.020. But the frequency generated by FreqMeasure.countToFrequency are the same 1600.00000. But I printed the result count/sum*16000000, it shows 1599.966064 1599.972168 1599.972778 1599.973755 1599.974487 1599.975342 1599.976318 1599.977051 1599.977905 1599.978516 1599.979614 1599.980225 1599.981079 1599.980835

quite linear with the input signl frequency. And the hardware actually could measure the tiny frequency difference. It seems some round operation is done in the librarary or some digitization issue. Is there a way to fix it?

And the frequency generated by FreqMeasure.countToFrequency is not a simple factor*count/sum, right?

Steps To Reproduce Problem

/* FreqMeasure - Example with serial output

void setup() { Serial.begin(115200); FreqMeasure.begin(); }

double sum=0; int count=0;

void loop() { if (FreqMeasure.available()) { // average several reading together sum = sum + FreqMeasure.read(); count = count + 1; if (count > 2000) { float frequency = FreqMeasure.countToFrequency(sum / count); Serial.println(frequency,12);

  Serial.println(count/sum*16000000,12);
  sum = 0;
  count = 0;
}

} }

Hardware & Software

Board: arduino uno and nano Shields / modules used Arduino IDE version 1.16 Teensyduino version (if using Teensy) Version info & package name (from Tools > Boards > Board Manager) Operating system & version Any other software or hardware?

PaulStoffregen commented 2 years ago

I do not understand what the issue or problem is, or what specifically needs to be fixed.

0i0i0i commented 2 years ago

Maybe I didn't describe it well. I would like to measure a small frequency difference, such as 0.01Hz or 0.001Hz. In principle, the mcu such as 328p together with a proper crystal can do that. So far, the official example program seems able to resolve 0.1Hz or so, by using FreqMeasure.countToFrequency(). Is there a way to increase the resolution? And when I check the count/sum value, it 0.001Hz difference can be resolved. So I guess there must be a way to convert this count/sum value into frequency without that 0.001Hz resolution.

PaulStoffregen commented 2 years ago

This issue tracker is for reporting software defects. It is the wrong place to ask for help.

Ask your question on the Arduino forum. If nobody gives a good answer after 1 week, post the link here and I may take a look.