This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
resources:tools-software:sigmastudio:toolbox:filters:firfilter [12 Jun 2020 01:00] Joshua Berlin Added python example code |
resources:tools-software:sigmastudio:toolbox:filters:firfilter [16 Jun 2020 03:15] Joshua Berlin Minor clarifications |
||
---|---|---|---|
Line 64: | Line 64: | ||
| | ||
print_object_property('FIR1', 'Coefficients', 0, 0) | print_object_property('FIR1', 'Coefficients', 0, 0) | ||
+ | </code> | ||
+ | |||
+ | ====== Controlling the FIR Filter from a Microcontroller ====== | ||
+ | The following assumes you have a working microcontroller platform, with SigmaDSP interface code based on [[resources:tools-software:sigmastudio:tutorials:microcontroller|Interfacing SigmaDSP Processors with a Microcontroller]]. | ||
+ | |||
+ | |||
+ | |||
+ | ===== Reading FIR Coefficients from the SigmaDSP ===== | ||
+ | <code cpp> | ||
+ | // NOTE: This code will not work for very large filters that span multiple data memories or memory pages. | ||
+ | void print_FIR_coeffs(int fir_start_addr, int fir_filter_length) { | ||
+ | // Initialize a double array to hold the coefficients. | ||
+ | double fir_coeffs[fir_filter_length] = {}; | ||
+ | |||
+ | for (int i = 0; i < fir_filter_length; i++) { | ||
+ | // DSP memory holds FIR coefficients in reverse order so fill the fir_coeffs array from last to first. | ||
+ | fir_coeffs[fir_filter_length - i - 1] = SIGMA_READ_REGISTER_FLOAT(fir_start_addr + i); | ||
+ | } | ||
+ | |||
+ | // At this point you can do anything you like with fir_coeffs. We will simply print them out. | ||
+ | Serial.println("FIR Coefficients:"); | ||
+ | for (int i = 0; i < fir_filter_length; i++) { | ||
+ | Serial.println(fir_coeffs[i], 4); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ===== Writing FIR Coefficients to the SigmaDSP ===== | ||
+ | <code cpp> | ||
+ | // NOTE: This code will not work for very large filters that span multiple data memories or memory pages. | ||
+ | void write_FIR_coeffs(int fir_start_addr, int fir_filter_length, double* coefficients) { | ||
+ | for (int i = 0; i < fir_filter_length; i++) { | ||
+ | // DSP memory holds FIR coefficients in reverse order, so increment address while decrementing coefficient index. | ||
+ | SIGMA_WRITE_REGISTER_FLOAT(fir_start_addr + i, coefficients[fir_filter_length - i - 1]); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ===== Example Usage ===== | ||
+ | If you have an FIR algorithm added to your SigmaStudio project, the exported source code will include variable information in the *PARAMS.h* file. Using these variables for addressing allows your code to automatically keep track of any changes in the filter address as your SigmaStudio project grows in complexity. | ||
+ | |||
+ | <code cpp> | ||
+ | // Print the coefficients contained in the default program | ||
+ | print_FIR_coeffs(MOD_FIR1_ALG0_FIRSIGMA300ALG1FIRCOEFF0_ADDR, MOD_FIR1_COUNT); | ||
+ | // Create an array of new coefficients | ||
+ | double new_coefficients[MOD_FIR1_COUNT] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0}; | ||
+ | // Write the new FIR coefficients to the DSP | ||
+ | write_FIR_coeffs(MOD_FIR1_ALG0_FIRSIGMA300ALG1FIRCOEFF0_ADDR, MOD_FIR1_COUNT, new_coefficients); | ||
+ | // Read back the coefficients; they should now be identical to new_coefficients | ||
+ | print_FIR_coeffs(MOD_FIR1_ALG0_FIRSIGMA300ALG1FIRCOEFF0_ADDR, MOD_FIR1_COUNT); | ||
</code> | </code> |