So, to fix it, cast i and max_i to double on lines 44 or 57, and remove the division by 2. Both the output frequency and the max frequency read (which should be close to SAMPLERATE value of 8000, and which was also halved) will be correct. ]]>

int frequency = 3560; // freq of our sine wave

output: 7120. r:0.0 i:-4000.000000000001 1780.0hz

The output frequency is half of the original frequency. Why?

]]>