Glitch in Spectrum Analysis

Feedback and Reviews for Audacity 2.x
Forum rules
This board is ONLY for general feedback and discussion about Audacity 2.X.

If you require help, or think you have found a "bug", please post on the forum board relevant to your operating system.
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
DickN
Posts: 445
Joined: Thu Jul 22, 2010 9:03 pm
Operating System: Windows Vista

Glitch in Spectrum Analysis

Post by DickN » Wed Jan 22, 2014 12:20 am

Using 2.0.5 Alpha 9/8/13. With this audio sample, using Bartlett window at length = 32768, one spectrum value is around +1200 dB. The exact frequency and amplitude change a bit when saved as a wav or flac file and imported back into the project, but frequency is in the 116-118 Hz range. Track settings are 41,100Hz and 32 bit float. The line does not show up in the plot nor does it seem to affect the scaling, but peaks to the left of it cannot be found. The glitch disappears if I change window or length setting.
Attachments
FFT error (32768, Bartlett).flac
Ambient noise sample, produces glitch in Spectrum plot with Bartlett window and length=32768. Doesn't appear in plot nor affect scaling, but peaks to the left of it cannot be found. Glitch is around 166-118 Hz, amplitude is over +1200dB.
(76.61 KiB) Downloaded 80 times

steve
Site Admin
Posts: 81627
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Glitch in Spectrum Analysis

Post by steve » Wed Jan 22, 2014 12:19 pm

How odd :?

I've had a look in the code and I think that I can see where it is coming from.
There is a note in the file FreqWindow.cpp

Code: Select all

// If f(0)=y0, f(1)=y1, f(2)=y2, and f(3)=y3, this function finds
// the degree-three polynomial which best fits these points and
// returns the value of this polynomial at a value x.  Usually
// 0 < x < 3
The problem seems to be that very occasionally the numbers will be such that the equation cannot be solved, and by chance you seem to have hit on such an occurrence.

A little further on in the code we have:

Code: Select all

   // Find zeroes of derivative using quadratic equation

   float discriminant = db * db - 4 * da * dc;
   if (discriminant < 0.0)
      return float(-1.0);              // error

   float x1 = (-db + sqrt(discriminant)) / (2 * da);
   float x2 = (-db - sqrt(discriminant)) / (2 * da);

   // The one which corresponds to a local _maximum_ in the
   // cubic is the one we want - the one with a negative
   // second derivative

   float dda = 2 * da;
   float ddb = db;
but with your audio sample the cubic is never calculated below 117 Hz because the discriminant is negative.

This has a knock on effect in FreqWindow::PlotPaint where the actual peak should be calculated.

It is possible that there could be an error in the equation.
I seem to get more sensible results changing lines 825 - 826 from

Code: Select all

            if (leftbin < 1)
               leftbin = 1;
to

Code: Select all

            if (leftbin < 1)
               leftbin = -1;
but sadly my maths is not good enough to know if that is correct or completely wrong.

The upshot is that there is definitely a "glitch", but whether it is because of an error in the code (a bug) or that the equation with that specific data has no solution, will take a better mathematician than myself to work out.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Post Reply