dsPIC FFT signal analysis issues

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
reab
Posts: 263
Joined: 28 Aug 2005 20:43
Location: West Sussex, United Kingdom
Contact:

dsPIC FFT signal analysis issues

#1 Post by reab » 27 Jul 2007 15:40

Hi all,

Using dsPICPRO2, dsPIC30F6014A device at 80mhz with GLCD

Using the example FFT_Test and the above hardware it all compiles OK - I haven't checked the functionality just yet but I did on a previous release and it worked reasonably well.

So, with the above hardware I'm trying to pull in parts of the FFT_Test.c example to my own project so that I can enhance and modify its functionality for signal analysis.

However . . I now get errors and I cannot understand where some of them are located so that I can resolve them.
For instance what is:

Code: Select all

Twiddle_Factors_Init();
It's part of the initialisation but I can't find it anywhere else in the project so where is it located so that I can understand and then reuse it? The P30F6014A.mlk file has the TwiddleFactors library present with:

Code: Select all

#pragma SetLib(__Lib_Matrices, __Lib_TwiddleFactors, __Lib_Vectors)
Also I get an error of:
const_TwiddleCoeff_256 not found
Its meant to be in an array in memory according to the help file but once again I can't find it in my project anywhere.

Anyone know of a solution to these issues?

Thanks

Roger B
Be kind and like your words - you never know, you may have to eat them one day!

Bob Lawrence
Posts: 300
Joined: 18 Aug 2004 11:55
Location: Lwr Sackville, Nova Scotia, Canada

TwiddleFactors in a complied library from Mikro Pascal

#2 Post by Bob Lawrence » 28 Jul 2007 12:40

Roger,

// Perform FFT (DFT), 7 stages, 128 samples of complex pairs
// Twiddle factors are taken from the <TwiddleFactors.dpas> unit
FFT(8, TwiddleCoeff_256, Samples);

the FFT library was was written in MikroPacal by Zoran. (I believe) and converted to MikroC.

I would try copying the library file "__Lib_TwiddleFactors.c " to your project folder from the \your directory \4.0\uses

I have FFT working in a test project

Bob Lawrence
Posts: 300
Joined: 18 Aug 2004 11:55
Location: Lwr Sackville, Nova Scotia, Canada

FFT(8, TwiddleCoeff_512, Samples);

#3 Post by Bob Lawrence » 28 Jul 2007 13:29

It should work but I don't lnow the ranges. There is additionalFFT info in the online DSP book. (link below)

Also a few comments from another post:
zristic
mikroElektronika team

PostPosted: Wed Jul 18, 2007 9:07 am Post subject: Re: FFT with

dsPIC30F3014 for musical instruments Reply with quote

Hi,

The example provided is very essential - it is for demonstratoin purposes. In order to get a better resolution, you will have to make more samples per second and to use as many twiddle factors as possible.
Also, you will have to make dsPIC run at maximum frequency, which is 120MHz (use 7.5MHz crystal with 16x PLL option turned on).

Here is a link to our online book which will help you to make fast ADC sampling:
http://www.mikroe.com/en/books/dspicbook/00.htm

reab
Posts: 263
Joined: 28 Aug 2005 20:43
Location: West Sussex, United Kingdom
Contact:

#4 Post by reab » 13 Aug 2007 12:07

Thanks for the reply Bob,

It seems that it cleared up when I copied the code across wholesale!

I now have an FFT display incorporated into my application that I can call up via the menu system showing gridlines, frequency etc

However I have a lack of understanding of the process as the display mirrors itself after the halfway position and comes back down in frequency! Most strange behaviour - looked through the ebook as suggested but there seemed little to help me on the FFT side!

Also there have been posting in these forum about the need to increase the values of some parameters - I'm expecting to analyse dc to 20khz with any luck. I've increased the buffer size from 512 to 1024 as the comments show in the example but with little difference!

Has anyone any experience of this?

I cannot open the twiddlefactors library by the way to change anything in there as was suggested in the Pascal forum for somenone else.

Any help appreciated.

Roger B
Be kind and like your words - you never know, you may have to eat them one day!

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

#5 Post by zristic » 27 Aug 2007 10:21

Code: Select all

//******************************************************************************
//**                                                                          **
//**   LIBRARY - TwiddleFactors                                               **
//**                                                                          **
//**                                                                          **
//**   mikroPascal for dsPIC compiler ver. 2.00 ++                            **
//**                                                                          **
//**   Author: zristic att mikroe.com                                         **
//**                                                                          **
//**   Description:                                                           **
//**      Twiddle factors are defined as                                      **
//**        exp(-j*2*pi*k/N)                                                  **
//**        k = 0, 1, ..., N-1                                                **
//**      Twiddle factors are represented in 1.15 radix point format (fractional)
//**        in sequence Re, Im, Re, Im...                                     **
//**                                                                          **
//**   History:                                                               **
//**       27.10.2005.                                                        **
//**         - ver 1.00                                                       **
//**                                                                          **
//**                                                                          **
//******************************************************************************
unit __Lib_TwiddleFactors;

const
  TwiddleCoeff_64: array[64] of word = (
    $7FFF, $0000, $7F62, $F374, $7D8A, $E707, $7A7D, $DAD8,
    $7642, $CF04, $70E3, $C3A9, $6A6E, $B8E3, $62F2, $AECC,
    $5A82, $A57E, $5134, $9D0E, $471D, $9592, $3C57, $8F1D,
    $30FC, $89BE, $2528, $8583, $18F9, $8276, $0C8C, $809E,
    $0000, $8000, $F374, $809E, $E707, $8276, $DAD8, $8583,
    $CF04, $89BE, $C3A9, $8F1D, $B8E3, $9592, $AECC, $9D0E,
    $A57E, $A57E, $9D0E, $AECC, $9592, $B8E3, $8F1D, $C3A9,
    $89BE, $CF04, $8583, $DAD8, $8276, $E707, $809E, $F374);

  TwiddleCoeff_128: array[128] of word = (
    $7FFF, $0000, $7FD9, $F9B8, $7F62, $F374, $7E9D, $ED38,
    $7D8A, $E707, $7C2A, $E0E6, $7A7D, $DAD8, $7885, $D4E1,
    $7642, $CF04, $73B6, $C946, $70E3, $C3A9, $6DCA, $BE32,
    $6A6E, $B8E3, $66D0, $B3C0, $62F2, $AECC, $5ED7, $AA0A,
    $5A82, $A57E, $55F6, $A129, $5134, $9D0E, $4C40, $9930,
    $471D, $9592, $41CE, $9236, $3C57, $8F1D, $36BA, $8C4A,
    $30FC, $89BE, $2B1F, $877B, $2528, $8583, $1F1A, $83D6,
    $18F9, $8276, $12C8, $8163, $0C8C, $809E, $0648, $8027,
    $0000, $8000, $F9B8, $8027, $F374, $809E, $ED38, $8163,
    $E707, $8276, $E0E6, $83D6, $DAD8, $8583, $D4E1, $877B,
    $CF04, $89BE, $C946, $8C4A, $C3A9, $8F1D, $BE32, $9236,
    $B8E3, $9592, $B3C0, $9930, $AECC, $9D0E, $AA0A, $A129,
    $A57E, $A57E, $A129, $AA0A, $9D0E, $AECC, $9930, $B3C0,
    $9592, $B8E3, $9236, $BE32, $8F1D, $C3A9, $8C4A, $C946,
    $89BE, $CF04, $877B, $D4E1, $8583, $DAD8, $83D6, $E0E6,
    $8276, $E707, $8163, $ED38, $809E, $F374, $8027, $F9B8);

  TwiddleCoeff_256: array[256] of word = (
    $7FFF, $0000, $7FF6, $FCDC, $7FD9, $F9B8, $7FA7, $F695,
    $7F62, $F374, $7F0A, $F055, $7E9D, $ED38, $7E1E, $EA1E,
    $7D8A, $E707, $7CE4, $E3F4, $7C2A, $E0E6, $7B5D, $DDDC,
    $7A7D, $DAD8, $798A, $D7D9, $7885, $D4E1, $776C, $D1EF,
    $7642, $CF04, $7505, $CC21, $73B6, $C946, $7255, $C673,
    $70E3, $C3A9, $6F5F, $C0E9, $6DCA, $BE32, $6C24, $BB85,
    $6A6E, $B8E3, $68A7, $B64C, $66D0, $B3C0, $64E9, $B140,
    $62F2, $AECC, $60EC, $AC65, $5ED7, $AA0A, $5CB4, $A7BD,
    $5A82, $A57E, $5843, $A34C, $55F6, $A129, $539B, $9F14,
    $5134, $9D0E, $4EC0, $9B17, $4C40, $9930, $49B4, $9759,
    $471D, $9592, $447B, $93DC, $41CE, $9236, $3F17, $90A1,
    $3C57, $8F1D, $398D, $8DAB, $36BA, $8C4A, $33DF, $8AFB,
    $30FC, $89BE, $2E11, $8894, $2B1F, $877B, $2827, $8676,
    $2528, $8583, $2224, $84A3, $1F1A, $83D6, $1C0C, $831C,
    $18F9, $8276, $15E2, $81E2, $12C8, $8163, $0FAB, $80F6,
    $0C8C, $809E, $096B, $8059, $0648, $8027, $0324, $800A,
    $0000, $8000, $FCDC, $800A, $F9B8, $8027, $F695, $8059,
    $F374, $809E, $F055, $80F6, $ED38, $8163, $EA1E, $81E2,
    $E707, $8276, $E3F4, $831C, $E0E6, $83D6, $DDDC, $84A3,
    $DAD8, $8583, $D7D9, $8676, $D4E1, $877B, $D1EF, $8894,
    $CF04, $89BE, $CC21, $8AFB, $C946, $8C4A, $C673, $8DAB,
    $C3A9, $8F1D, $C0E9, $90A1, $BE32, $9236, $BB85, $93DC,
    $B8E3, $9592, $B64C, $9759, $B3C0, $9930, $B140, $9B17,
    $AECC, $9D0E, $AC65, $9F14, $AA0A, $A129, $A7BD, $A34C,
    $A57E, $A57E, $A34C, $A7BD, $A129, $AA0A, $9F14, $AC65,
    $9D0E, $AECC, $9B17, $B140, $9930, $B3C0, $9759, $B64C,
    $9592, $B8E3, $93DC, $BB85, $9236, $BE32, $90A1, $C0E9,
    $8F1D, $C3A9, $8DAB, $C673, $8C4A, $C946, $8AFB, $CC21,
    $89BE, $CF04, $8894, $D1EF, $877B, $D4E1, $8676, $D7D9,
    $8583, $DAD8, $84A3, $DDDC, $83D6, $E0E6, $831C, $E3F4,
    $8276, $E707, $81E2, $EA1E, $8163, $ED38, $80F6, $F055,
    $809E, $F374, $8059, $F695, $8027, $F9B8, $800A, $FCDC);

const TwiddleCoeff_512: array[512] of word = (
    $7FFF, $0000, $7FFE, $FE6E, $7FF6, $FCDC, $7FEA, $FB4A,
    $7FD9, $F9B8, $7FC2, $F827, $7FA7, $F695, $7F87, $F505,
    $7F62, $F374, $7F38, $F1E4, $7F0A, $F055, $7ED6, $EEC6,
    $7E9D, $ED38, $7E60, $EBAB, $7E1E, $EA1E, $7DD6, $E892,
    $7D8A, $E707, $7D3A, $E57D, $7CE4, $E3F4, $7C89, $E26D,
    $7C2A, $E0E6, $7BC6, $DF61, $7B5D, $DDDC, $7AEF, $DC59,
    $7A7D, $DAD8, $7A06, $D958, $798A, $D7D9, $790A, $D65C,
    $7885, $D4E1, $77FB, $D367, $776C, $D1EF, $76D9, $D079,
    $7642, $CF04, $75A6, $CD92, $7505, $CC21, $7460, $CAB2,
    $73B6, $C946, $7308, $C7DB, $7255, $C673, $719E, $C50D,
    $70E3, $C3A9, $7023, $C248, $6F5F, $C0E9, $6E97, $BF8C,
    $6DCA, $BE32, $6CF9, $BCDA, $6C24, $BB85, $6B4B, $BA33,
    $6A6E, $B8E3, $698C, $B796, $68A7, $B64C, $67BD, $B505,
    $66D0, $B3C0, $65DE, $B27F, $64E9, $B140, $63EF, $B005,
    $62F2, $AECC, $61F1, $AD97, $60EC, $AC65, $5FE4, $AB36,
    $5ED7, $AA0A, $5DC8, $A8E2, $5CB4, $A7BD, $5B9D, $A69C,
    $5A82, $A57E, $5964, $A463, $5843, $A34C, $571E, $A238,
    $55F6, $A129, $54CA, $A01C, $539B, $9F14, $5269, $9E0F,
    $5134, $9D0E, $4FFB, $9C11, $4EC0, $9B17, $4D81, $9A22,
    $4C40, $9930, $4AFB, $9843, $49B4, $9759, $486A, $9674,
    $471D, $9592, $45CD, $94B5, $447B, $93DC, $4326, $9307,
    $41CE, $9236, $4074, $9169, $3F17, $90A1, $3DB8, $8FDD,
    $3C57, $8F1D, $3AF3, $8E62, $398D, $8DAB, $3825, $8CF8,
    $36BA, $8C4A, $354E, $8BA0, $33DF, $8AFB, $326E, $8A5A,
    $30FC, $89BE, $2F87, $8927, $2E11, $8894, $2C99, $8805,
    $2B1F, $877B, $29A4, $86F6, $2827, $8676, $26A8, $85FA,
    $2528, $8583, $23A7, $8511, $2224, $84A3, $209F, $843A,
    $1F1A, $83D6, $1D93, $8377, $1C0C, $831C, $1A83, $82C6,
    $18F9, $8276, $176E, $822A, $15E2, $81E2, $1455, $81A0,
    $12C8, $8163, $113A, $812A, $0FAB, $80F6, $0E1C, $80C8,
    $0C8C, $809E, $0AFB, $8079, $096B, $8059, $07D9, $803E,
    $0648, $8027, $04B6, $8016, $0324, $800A, $0192, $8002,
    $0000, $8000, $FE6E, $8002, $FCDC, $800A, $FB4A, $8016,
    $F9B8, $8027, $F827, $803E, $F695, $8059, $F505, $8079,
    $F374, $809E, $F1E4, $80C8, $F055, $80F6, $EEC6, $812A,
    $ED38, $8163, $EBAB, $81A0, $EA1E, $81E2, $E892, $822A,
    $E707, $8276, $E57D, $82C6, $E3F4, $831C, $E26D, $8377,
    $E0E6, $83D6, $DF61, $843A, $DDDC, $84A3, $DC59, $8511,
    $DAD8, $8583, $D958, $85FA, $D7D9, $8676, $D65C, $86F6,
    $D4E1, $877B, $D367, $8805, $D1EF, $8894, $D079, $8927,
    $CF04, $89BE, $CD92, $8A5A, $CC21, $8AFB, $CAB2, $8BA0,
    $C946, $8C4A, $C7DB, $8CF8, $C673, $8DAB, $C50D, $8E62,
    $C3A9, $8F1D, $C248, $8FDD, $C0E9, $90A1, $BF8C, $9169,
    $BE32, $9236, $BCDA, $9307, $BB85, $93DC, $BA33, $94B5,
    $B8E3, $9592, $B796, $9674, $B64C, $9759, $B505, $9843,
    $B3C0, $9930, $B27F, $9A22, $B140, $9B17, $B005, $9C11,
    $AECC, $9D0E, $AD97, $9E0F, $AC65, $9F14, $AB36, $A01C,
    $AA0A, $A129, $A8E2, $A238, $A7BD, $A34C, $A69C, $A463,
    $A57E, $A57E, $A463, $A69C, $A34C, $A7BD, $A238, $A8E2,
    $A129, $AA0A, $A01C, $AB36, $9F14, $AC65, $9E0F, $AD97,
    $9D0E, $AECC, $9C11, $B005, $9B17, $B140, $9A22, $B27F,
    $9930, $B3C0, $9843, $B505, $9759, $B64C, $9674, $B796,
    $9592, $B8E3, $94B5, $BA33, $93DC, $BB85, $9307, $BCDA,
    $9236, $BE32, $9169, $BF8C, $90A1, $C0E9, $8FDD, $C248,
    $8F1D, $C3A9, $8E62, $C50D, $8DAB, $C673, $8CF8, $C7DB,
    $8C4A, $C946, $8BA0, $CAB2, $8AFB, $CC21, $8A5A, $CD92,
    $89BE, $CF04, $8927, $D079, $8894, $D1EF, $8805, $D367,
    $877B, $D4E1, $86F6, $D65C, $8676, $D7D9, $85FA, $D958,
    $8583, $DAD8, $8511, $DC59, $84A3, $DDDC, $843A, $DF61,
    $83D6, $E0E6, $8377, $E26D, $831C, $E3F4, $82C6, $E57D,
    $8276, $E707, $822A, $E892, $81E2, $EA1E, $81A0, $EBAB,
    $8163, $ED38, $812A, $EEC6, $80F6, $F055, $80C8, $F1E4,
    $809E, $F374, $8079, $F505, $8059, $F695, $803E, $F827,
    $8027, $F9B8, $8016, $FB4A, $800A, $FCDC, $8002, $FE6E);

implementation

end.
Last edited by zristic on 28 Oct 2009 10:16, edited 1 time in total.

JosePoloramos
Posts: 2
Joined: 28 Oct 2009 02:59

Copying the twiddle factors

#6 Post by JosePoloramos » 28 Oct 2009 03:18

From reading your post then all I have to do is copy the library into the project folder. could that simply be done by opening a new file within the project and pasting the twiddle factors into the file.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Copying the twiddle factors

#7 Post by zristic » 28 Oct 2009 09:54

JosePoloramos wrote:From reading your post then all I have to do is copy the library into the project folder. could that simply be done by opening a new file within the project and pasting the twiddle factors into the file.
Yes, just do not forget to add "uses __Lib_TwiddleFactors;" in your main program or in the unit which will actually uses the lib.

JosePoloramos
Posts: 2
Joined: 28 Oct 2009 02:59

Copying the twiddle factors

#8 Post by JosePoloramos » 28 Oct 2009 18:10

this is in pascal but I can modify to C am I correct. Then I copy the library into a .C file within the project and "uses __Lib_TwiddleFactors;"

as far as the input is concerned would it be more efficient to pass a complex array?

ie.. create a complex struct with 2 data parameters real and Imaginary or just pass an float array with every other index set to 0;

Sorry to ask so many questions i never had to use the FFT function before; I am building a pulse oximeter and wish to obtain the saturation values through spectral analysis..

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: Copying the twiddle factors

#9 Post by zristic » 29 Oct 2009 10:03

Well, there are many ways to import constants into mikroC project. What I would do is to make a separate C file, save it into the project folder and add it to be a part of the project.

Now, how you will organize the constants, whether it will be a const structure or a simple const array or something else, it is up to you and your preferences.

Post Reply

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