Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple of programming changes, first of all:</p> <pre><code>xAxis += pieSteps; if (xAxis &gt;= fullSinWave) xAxis -= fullSinWave; //wrap x back into 0-2pi period </code></pre> <p>will help reduce numeric error.</p> <pre><code>in_buf[i].r = dataStream[i]; in_buf[i].i = 0; </code></pre> <p>will set the input buffer to <code>sin(x)</code>, previously you had it set to <code>sin(x) + j*sin(x)</code>, where <code>j = sqrt(-1)</code>.</p> <p>Moving <code>wantedHz = 1000;</code> out of the loop looks better.</p> <p>And a more fundamental issue: you set <code>wantedHz = 1000</code>. With a sample rate of 44.1 kHz this corresponds to <code>44100 points/sec * (1/1000) sec/cycle = 44.1 points/cycle</code>. With a buffer of 512 points, you will get 11.6 cycles of the sine wave in the buffer. Non-integer cycles lead to <a href="http://en.wikipedia.org/wiki/Spectral_leakage" rel="nofollow">leakage</a>. </p> <p>Before getting into this, though, try setting <code>wantedHz = 12*44100.0/512</code> to give exactly 12 cycles in the buffer. You should see two spikes in the transform: one at index 12, and one at index 511-12.</p> <p>The reason you'll see two spikes is that the transform of <code>sin(w_0*x)</code> is <code>j*{-delta(w-w_0) - delta(w+w_0)}</code>. That is, you get an impulse function at w_0 and -w_0 in the imaginary part of the transform. The reason they are at the places they are is that the transform goes from 0 to 2*pi.</p> <p>After you do this, go back to <code>wantedH = 1000</code>, giving you a non-integer number of cycles in the buffer. You should see a wide tent-shaped result, centered around bins 11 and 511-11. You should multiply <code>dataStream</code> by a window function (<a href="http://en.wikipedia.org/wiki/Hann_function" rel="nofollow">Hann</a> is good) to reduce the impact of this effect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload