dsPIC FFT example improvement

General discussion on mikroPascal for dsPIC30/33 and PIC24.
Post Reply
Author
Message
yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

dsPIC FFT example improvement

#1 Post by yo2lio » 27 Sep 2008 21:15

Hello,

I make an improvement in FFT example.

Instead of this code, in WriteData routine :

Code: Select all

      tmpR := sqrt(Rer + Imr);      // Amplitude of current DFT sample
      Rer  := tmpR*256.;            // DFT is scaled down by 1/N, we need to
                                    //   take it back in order to have visible
                                    //   components on GLCD

      Re := Rer;
I put this code :

Code: Select all

Re := sqrt_int(dword(Rer + Imr)*65536);
Where sqrt_int routine is :

Code: Select all

function sqrt_int(num : dword) : dword;
var one : dword;
begin
  result := 0;
  one := 1 shl 30;
  while one > num do one := one shr 2;
  while one > 0 do
    begin
      if num >= (result + one) then
        begin
          num := num - (result + one);
          result := result + (one shl 1);
        end;
      result := result shr 1;
      one := one shr 2;
    end;
end;
sqrt_int routine, is not my invention, I found this on the Google ....

With this improvement, WriteData routine is 4 time faster !!!

Enjoy !
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

SamY Fareast
Posts: 46
Joined: 05 Aug 2007 07:15
Location: Shizuoka JAPAN

#2 Post by SamY Fareast » 09 Mar 2009 15:37

GREAT!

But typo?
I put this code :
Code:

Code: Select all

Re := sqrt_int(dword(Rer + Imr)*65536);
By 'Fract2Float' function,
On this timing, 0<= Rer, Imr < 1, then dword(Rer+Imr) is almost 0.

Code: Select all

Re := (Rer + Imr) * 65535;
Re := sqr_int(dword(Re));
May work, But if you use 'sqr_int ' routine, there is no needs for use Real type anymore.

I wrote another modification to 'Write_Data' on http://www.mikroe.com/forum/viewtopic.php?t=18699.
Please check it.

Post Reply

Return to “mikroPascal for dsPIC30/33 and PIC24 General”