Skip to content

Commit 51ed722

Browse files
Vineeth Krishna Rharishagari
Vineeth Krishna R
authored andcommitted
Pull request #30: Develop
Merge in MCU16CE/dspic33e-code-examples from develop to master * commit '7b773db5009bab64395d9bdf7e51a42dd7ea358c': MCU16GITHUB-1278 - Updated version number in changelog MCU16GITHUB-1278 - Update main.json with version number MCU16GITHUB-1278 - Updated readme, main.json and changelog MCU16GITHUB-1278 - FFT Twid factor corrected. MCU16GITHUB-1278 - FFT peakFrequency calculation corrected. updated jenkinsfile
2 parents 7720960 + 7b773db commit 51ed722

File tree

11 files changed

+212
-120
lines changed

11 files changed

+212
-120
lines changed

.citd/Jenkinsfilek8s

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pipeline {
5353
MPLABX_PROJECT_SOURCE = "../"
5454
}
5555

56+
triggers {
57+
cron(env.BRANCH_NAME == 'develop' ? 'H H 1 * *': '')
58+
}
5659
options {
5760
timestamps()
5861
timeout(time: 45, unit: 'MINUTES')

.main-meta/main.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"content":{
55
"metaDataVersion":"1.3.0",
66
"name":"com.microchip.mplabx.project.dspic33e-code-examples",
7-
"version":"1.0.0",
7+
"version":"1.0.1",
88
"displayName":"dsPIC33E Code Examples",
99
"projectName":"dspic33e-code-examples",
1010
"shortDescription":"dsPIC33E Code Examples",

changelog.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# dsPIC33E Code Examples v1.0.1
2+
### Release Highlights
3+
4+
Minor corrections on [FFT code example](dspic33e-fft-dsplib).
5+
6+
### Features Added\Updated
7+
8+
* Updated peakFrequency calculation formula to avoid rounding errors.
9+
* Corrected twiddle factors initialization in [twiddle_factors.c](dspic33e-fft-dsplib/firmware/src/twiddle_factors.c) for FFT lengths of 64 to 256.
10+
11+
112
# dsPIC33E Code Examples v0.1.0
213
### Release Highlights
314

dspic33e-fft-dsplib/.main-meta/main.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"content":{
55
"metaDataVersion":"1.3.0",
66
"name":"com.microchip.mplabx.project.dspic33e-fft-dsplib",
7-
"version":"1.0.0",
8-
"displayName":"FFT dsplib",
7+
"version":"1.0.1",
8+
"displayName":"FFT implementataion in dsPIC33E",
99
"projectName":"dspic33e-fft-dsplib",
10-
"shortDescription":"FFT dsplib",
10+
"shortDescription":"Example code for dsPIC33E devices, demonstrating the implementation of FFT operations using the XC16/XC-DSC DSP Library.",
1111
"ide":{
1212
"name":"MPLABX",
1313
"semverRange":">=6.00"
@@ -18,7 +18,7 @@
1818
},
1919
"dfp":{
2020
"name":"dsPIC33E-GM-GP-MC-GU-MU_DFP",
21-
"semverRange":"^1.3.85"
21+
"semverRange":"^1.7.316"
2222
},
2323
"device":{
2424
"metaDataVersion":"1.0.0",
@@ -33,7 +33,9 @@
3333
"peripherals":[
3434
],
3535
"keywords":[
36-
"DSP"
36+
"DSP",
37+
"FFT",
38+
"Algorithm"
3739
]
3840
}
3941
}

dspic33e-fft-dsplib/README.md

+93-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![image](../images/microchip.jpg)
22

3-
## FFT DSPLIB
3+
## FFT Implementation using DSP Library
44

55
## Description:
66

@@ -10,42 +10,108 @@ Multiply-accumulate (MAC) type instructions and the ability to store and retriev
1010

1111
Microchip provides a DSP functions library that provides in-place FFT functions.
1212

13-
In this code example, we demonstrate how the DSP library functions can be used to perform an FFT on an input signal (vector). The code example is
14-
reconfigurable to perfrom an FFT of any size, including common sizes of 64, 128, 256 and 512 points. The code example also allows the user to place
13+
This code example demonstrates how the DSP library functions can be used to perform an FFT operation on an input signal (vector) and find the spectral component with the highest energy.
14+
15+
## Configuration
16+
17+
### Reconfiguring the project for a different dsPIC33E device:
18+
19+
The Project can be easily reconfigured for dspic33ep512gm710/dspic33ep512mu810/dspic33ep256gp506 device by following the below steps -
20+
1. Open project in MPLAB-X.
21+
2. In MPLAB X>>Configuration drop-down option>>Listed Device Configuration
22+
3. Re-build the MPLAB® project using the menu option: Clean and Build Main Project
23+
24+
### Reconfiguring the project for a different FFT Size:
25+
26+
The code example is reconfigurable to perfrom an FFT of any size, including common sizes of 64, 128, 256 and 512 points. The code example also allows the user to place
1527
the FFT coefficients (known as Twiddle Factors) in RAM or in Program Flash Memory. The project may be easily reconfigured by modifying the header
16-
file, FFT.h. By default, the example implements a 256-point FFT using coefficients stored in Program Flash memory.
28+
file, [fft.h](firmware/src/fft.h).
29+
- Change `FFT_BLOCK_LENGTH` to either 64, 128, 256 or 512
30+
- Correspondingly, change `LOG2_BLOCK_LENGTH` to either 6, 7, 8 or 9 respectively.
31+
- If you would like to store Twiddle Factors coefficients in RAM instead of Program Memory comment out the line - `#define FFTTWIDCOEFFS_IN_PROGMEM`
1732

18-
The input signal for our example will be 256 points of a Square wave signal of frequency 1KHz sampled at 10 KHz. This signal was first generated by
19-
dsPICworks and then exported as an assembler file from dsPICworks. After exporting it out, the assembler file was modified to ensure the samples
20-
reside in Y-data space.
33+
## Operation
2134

22-
The FFT operation is performed on the input signal, in-place. This means that the output of the FFT resides in the same RAM locations where the
23-
input signal used to reside. The FFT is performed in the following steps:
35+
The FFT is performed in the following steps:
36+
37+
1. Initialization:
38+
39+
i. Generate Twiddle Factor Coefficients and store them in X-RAM. The twiddle factors can be generated using the following function:
40+
```C
41+
#ifndef FFTTWIDCOEFFS_IN_PROGMEM /* Generate TwiddleFactor Coefficients */
42+
TwidFactorInit (LOG2_BLOCK_LENGTH, &twiddleFactors[0], 0); /* We need to do this only once at start-up */
43+
#endif
44+
```
45+
46+
Alternatively, you may also use the pre-generated twiddle factors stored in Program Flash.
47+
48+
ii. Initialize the input vector in Y-memory at an address aligned to `4 x FFT_BLOCK_LENGTH` as shown below.
49+
50+
```C
51+
fractcomplex sigCmpx[FFT_BLOCK_LENGTH] __attribute__ ((eds, space(ymemory), aligned (FFT_BLOCK_LENGTH * 2 *2)));
52+
```
53+
54+
iii. Scale the input signal to lie within the range _[-0.5, +0.5]_. For fixed point fractional input data, this translates to input samples in
55+
the range _[0xC000,0x3FFF]_. The scaling is achieved by simply right-shifting the input samples by 1 bit, assuming the input samples lie
56+
in the fixed point range _[0x8000,0x7FFF]_ or _[-1,+1)_.
57+
58+
iv. Convert the real input signal vector to a complex vector by placing zeros in every other location to signify a complex input whose
59+
imaginary part is _0x0000_.
2460

25-
1.Initialization: Generate Twiddle Factor Coefficients and store them in X-RAM or alternately use twiddle factor coefficients stored in
26-
Program Flash.
2761

28-
2.Scale the input signal to lie within the range [-0.5, +0.5]. For fixed point fractional input data, this translates to input samples in
29-
the range [0xC000,0x3FFF]. The scaling is achieved by simply right-shifting the input samples by 1 bit, assuming the input samples lie
30-
in the fixed point range [0x8000,0x7FFF] or [-1,+1).
62+
2. Butterfly computation:
63+
64+
This is achieved by performing a call to the FFTComplexIP() function.
65+
```C
66+
#ifndef FFTTWIDCOEFFS_IN_PROGMEM
67+
FFTComplexIP (LOG2_BLOCK_LENGTH, &sigCmpx[0], &twiddleFactors[0], COEFFS_IN_DATA);
68+
#else
69+
FFTComplexIP (LOG2_BLOCK_LENGTH, &sigCmpx[0], (fractcomplex *) __builtin_psvoffset(&twiddleFactors[0]), (int) __builtin_psvpage(&twiddleFactors[0]));
70+
#endif
71+
```
3172

32-
3.Convert the real input signal vector to a complex vector by placing zeros in every other location to signify a complex input whose
33-
imaginary part is 0x0000.
3473

35-
4.Butterfly computation: This is achieved by performing a call to the FFTComplexIP() function.
74+
3. Bit-Reversed Re-ordering: The output array is re-ordered to be in bit-reversed order of the addresses. This is achieved by -
75+
```C
76+
/* Store output samples in bit-reversed order of their addresses */
77+
BitReverseComplex (LOG2_BLOCK_LENGTH, &sigCmpx[0]);
78+
```
3679
37-
5.Bit-Reversed Re-ordering: The output array is re-ordered to be in bit-reversed order of the addresses. This is achieved by a function
38-
call to BitReverseComplex().
3980
40-
6.SquareMagnitude computation: We then need to compute the magnitude of each complex element in the output vector, so that we can estimate
41-
the energy in each spectral component/frequency bin. This is achieved by a call to a special C-callable routine, SquareMagnitudeCplx(),
42-
written in assembler language. This routine will be incorporated into the DSP library in future revisions of the C30 toolsuite. At that time,
43-
you may remove the source file, cplxsqrmag.s from the project and include the latest DSP library file, libdsp-coff.a.
81+
4. SquareMagnitude computation: We then need to compute the magnitude of each complex element in the output vector, so that we can estimate
82+
the energy in each spectral component/frequency bin. This is achieved by a call to a DSP Library's routine, SquareMagnitudeCplx().
83+
```C
84+
/* Compute the square magnitude of the complex FFT output array so we have a Real output vetor */
85+
SquareMagnitudeCplx(FFT_BLOCK_LENGTH/2, &sigCmpx[0], output);
86+
```
87+
88+
89+
5. Peak-picking: We then find the frequency component with the largest energy by using the VectorMax() routine in the DSP library.
90+
```C
91+
/* Find the frequency Bin ( = index into the SigCmpx[] array) that has the largest energy i.e., the largest spectral component */
92+
VectorMax(FFT_BLOCK_LENGTH/2, output, &peakFrequencyBin);
93+
```
94+
95+
96+
6. Frequency Calculation: The value of the spectral component with the highest energy, in Hz, is calculated by multiplying the array index of
97+
the largest element in the output array with the spectral (bin) resolution as follows -
98+
```C
99+
/* Compute the frequency (in Hz) of the largest spectral component */
100+
peakFrequency = (uint32_t) peakFrequencyBin*((float)SAMPLING_RATE/FFT_BLOCK_LENGTH);
101+
```
102+
103+
104+
### Input
105+
106+
The input signal used in the example will be 512 points of a Square wave signal of frequency 1KHz sampled at 10 KHz.
107+
108+
### Output
109+
The FFT operation is performed on the input signal, in-place. This means that the output of the FFT resides in the same RAM locations where the
110+
input signal used to reside.
111+
112+
Observe output variable `peakFrequency` in debug mode. Value should be almost near to 1Khz
44113

45-
7.Peak-picking: We then find the frequency component with the largest energy by using the VectorMax() routine in the DSP library.
46114

47-
8.Frequency Calculation: The value of the spectral component with the highest energy, in Hz, is calculated by multiplying the array index of
48-
the largest element in the output array with the spectral (bin) resolution ( = sampling rate/FFT size).
49115

50116
## Hardware Used
51117

@@ -57,4 +123,4 @@ the largest element in the output array with the spectral (bin) resolution ( = s
57123

58124
- MPLAB® X IDE v6.00 or newer (https://www.microchip.com/mplabx)
59125
- MPLAB® XC16 v2.00 or newer (https://www.microchip.com/xc)
60-
126+
- MPLAB® XC16/XC-DSC DSP Library. Documentation can be found at `compiler_installation_folder/docs/dsp_lib`

0 commit comments

Comments
 (0)