I'm trying to calculate the spectral density of a signal. What I'm expecting is a scale in dbFS where 0 dbFS would be the maximal theorical energy if all my signal is concentrated in a single frequency (without clipping, of course).
So, I'll calculate a ratio :
float dbfs = 20.Math.log10(freq_energy/max_theorical_energy);
As an example, I'm using a 16 bits signal sampled at 44100 Hz with 2048 samples for each "sound packet". To get that maximal theorical energy, I suppose I generate a signal at 44100/2048 Hz (around 21 Hz). So, in one "sound packet", I will get one single complete sine cycle. And, as temporal_energy = frequency_energy (Parseval Theorem), I used that routine to find that max_theorical_energy, based on the energy of that theorical temporal signal :
double max_energy
for (int i=0 ; i<2048 ; i++) {
max_energy+=Math.pow( Math.abs ( Math.sin(2*Math.Pi/2048) * Math.pow(2, 15) ), 2);
//2^15 = 32000 = maximum absolute value of a 16 bit signal
}
Then, I generated signals at a given frequencie (210 Hz for example) and compared the energy of that signal to my theorical result. As the values for each frequencies are complex, I used that routine to get the energy of my signal (still generating 2048 samples for each "sound packet) :
double signal_energy = 0d;
// datas[] represent the values of my signal after a FFT
for (int i = 0 ; i<datas.length ; i++) {
signal_energy+= Math.pow(datas[i].getReal(), 2) + Math.pow(datas[i].getImaginary(), 2)
}
So, now, if I calculate the ratio signal_energy / max_theorical_energy, I'm expecting something around 1. But the result is 1.xxxxE8 ! It's very far from 1...
So, what's wrong ? Where are my mistakes ?
Thank you for your help.