Commit ecc9e1d7b2c2d07c1d9673e8f11b9f1d1cf53ebd

Authored by oliverhaag
1 parent 3c06156d

Improved frequency detection routine

openhantek/ChangeLog
... ... @@ -105,3 +105,4 @@
105 105  
106 106 2010-12-13 Oliver Haag <oliver.haag@gmail.com>
107 107 * Triggermode Auto working correctly now
  108 +* Improved frequency detection routine
... ...
openhantek/src/dataanalyzer.cpp
... ... @@ -338,42 +338,23 @@ void DataAnalyzer::run() {
338 338 this->analyzedData[channel]->amplitude = maximalVoltage - minimalVoltage;
339 339  
340 340 // Get the frequency from the correlation results
341   - double correlationLimit = pow(sqrt(maximalVoltage - minimalVoltage) / 2, 4);
342   - bool newPeak = false; // Ignore correlation without offset (position = 0)
343   - double bestPeak = 0, lastPeak = 0;
344   - unsigned int bestPeakPosition = 0, currentPeakPosition = 0;
  341 + double minimumCorrelation = correlation[0];
  342 + double peakCorrelation = 0;
  343 + unsigned int peakPosition = 0;
345 344  
346   - for(unsigned int position = 1; position < this->analyzedData[channel]->samples.voltage.count; position++) {
347   - if(correlation[position] < correlationLimit) {
348   - // Check if there was a good peak before
349   - if(currentPeakPosition) {
350   - // Is this really a better correlation and not just a secondary peak of the first one?
351   - if(lastPeak > bestPeak * 1.2) {
352   - bestPeak = lastPeak;
353   - bestPeakPosition = currentPeakPosition;
354   - }
355   - currentPeakPosition = 0;
356   - }
357   - newPeak = true;
358   - }
359   - else if((currentPeakPosition || newPeak) && correlation[position] > lastPeak) {
360   - // We want this peak, store it
361   - lastPeak = correlation[position];
362   - currentPeakPosition = position;
363   - newPeak = false;
  345 + for(unsigned int position = 1; position < this->analyzedData[channel]->samples.voltage.count / 2; position++) {
  346 + if(correlation[position] > peakCorrelation && correlation[position] > minimumCorrelation * 2) {
  347 + peakCorrelation = correlation[position];
  348 + peakPosition = position;
364 349 }
  350 + else if(correlation[position] < minimumCorrelation)
  351 + minimumCorrelation = correlation[position];
365 352 }
366 353 delete[] correlation;
367 354  
368   - // Check if there's a possible peak available that wasn't finished
369   - if(currentPeakPosition && currentPeakPosition < this->analyzedData[channel]->samples.voltage.count - 1 && lastPeak > bestPeak * 1.2) {
370   - bestPeak = lastPeak;
371   - bestPeakPosition = currentPeakPosition;
372   - }
373   -
374 355 // Calculate the frequency in Hz
375   - if(bestPeakPosition)
376   - this->analyzedData[channel]->frequency = 1.0 / (this->analyzedData[channel]->samples.voltage.interval * bestPeakPosition);
  356 + if(peakPosition)
  357 + this->analyzedData[channel]->frequency = 1.0 / (this->analyzedData[channel]->samples.voltage.interval * peakPosition);
377 358 else
378 359 this->analyzedData[channel]->frequency = 0;
379 360  
... ...