Forum Discussion
Altera_Forum
Honored Contributor
16 years agoNow it seems to work. But I don't know the correct reason why. Let me describe.
First of all I use a Nios II processor to handle the data of the FFT. I don't scale anything in hardware. Performance is not a problem right know. I wrote a wrapper around the FFT instance so that the real and imaginary outputs are merged to a 32bit bus and the exponent can be read separatetly. here my C-code for the Scaling: samples_u32arr[] is an static array holding all 1024 32bit values (16bit real+16bit imag) i read from the fft module. number_of_samples = 1024. for(samplecounter_u16 = 0; samplecounter_u16 < number_of_samples; samplecounter_u16++) { real_16arr[samplecounter_u16] = (int16_t)samples_u32arr[samplecounter_u16] & 0xffff; imag_16arr[samplecounter_u16] = (int16_t)((samples_u32arr[samplecounter_u16] >> 16) & 0xffff); real_32arr[samplecounter_u16] = ((int32_t)real_16arr[samplecounter_u16]) << -exponent_s8; imag_32arr[samplecounter_u16] = ((int32_t)imag_16arr[samplecounter_u16]) << -exponent_s8; real_16 = (int16_t)((real_32arr[samplecounter_u16] >> 9) & 0xffff); imag_16 = (int16_t)((imag_32arr[samplecounter_u16] >> 9) & 0xffff); result_u16arr[samplecounter_u16] = (uint16_t)sqrt(real_16*real_16 + imag_16*imag_16); } The strange thing is, that I have to shift the data right 9 times, after I shifted it left with the value of the exponent, which is changing with the input data. I don't know why exactly the number 9!!! But if I do shift 9 times right and choose the lower 16bit the output amplitude corresponds to the input amplitude, with small deviations. I tested the scaling with a sine and a rectangular waveform with different amplitudes at the same frequency of 9Hz (3kHz sample rate). So there are no additional deviations caused by the window function (in this case just a rectangle).