add some code
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(iir)
|
||||
107
managed_components/espressif__esp-dsp/examples/iir/README.md
Normal file
107
managed_components/espressif__esp-dsp/examples/iir/README.md
Normal file
@@ -0,0 +1,107 @@
|
||||
# IIR Filter Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example demonstrates how to use IIR filters functionality from esp-dsp library. Example does the following steps:
|
||||
|
||||
1. Initialize the library
|
||||
2. Initialize input signal
|
||||
3. Show LPF filter with Q factor 1
|
||||
* Calculate iir filter coefficients
|
||||
* Filter the input test signal (delta function)
|
||||
* Shows impulse response on the plot
|
||||
* Shows frequency response on the plot
|
||||
* Calculate execution performance
|
||||
4. The same for LPF filter with Q factor 10
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware required
|
||||
|
||||
This example does not require any special hardware, and can be run on any common development board.
|
||||
|
||||
### Configure the project
|
||||
|
||||
Under Component Config ---> DSP Library ---> DSP Optimization, it's possible to choose either the optimized or ANSI implementation, to compare them.
|
||||
|
||||
### Build and flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output (replace PORT with serial port name):
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example output
|
||||
|
||||
Here is an typical example console output.
|
||||
|
||||
```
|
||||
I (58) main: Start Example.
|
||||
I (58) main: Impulse response of IIR filter with F=0.100000, qFactor=1.000000
|
||||
I (68) view: Data min[8] = -0.060052, Data max[2] = 0.333517
|
||||
________________________________________________________________
|
||||
0 |
|
||||
1 |
|
||||
2 - |
|
||||
3- - |
|
||||
4 -------------------------------------------------------------|
|
||||
5 |
|
||||
6 |
|
||||
7 |
|
||||
8 |
|
||||
9 |
|
||||
0123456789012345678901234567890123456789012345678901234567890123
|
||||
I (138) view: Plot: Length=128, min=-1.000000, max=1.000000
|
||||
I (148) view: Data min[511] = -149.983795, Data max[0] = 0.000000
|
||||
________________________________________________________________
|
||||
0 |
|
||||
1 |
|
||||
2----------------- |
|
||||
3 ---------- |
|
||||
4 ------------- |
|
||||
5 ---------- |
|
||||
6 ------- |
|
||||
7 --- |
|
||||
8 -- |
|
||||
9 --|
|
||||
0123456789012345678901234567890123456789012345678901234567890123
|
||||
I (228) view: Plot: Length=512, min=-100.000000, max=0.000000
|
||||
I (228) main: IIR for 1024 samples take 20276 cycles
|
||||
I (238) main: Impulse response of IIR filter with F=0.100000, qFactor=10.000000
|
||||
I (248) view: Data min[7] = -0.453739, Data max[2] = 0.526114
|
||||
________________________________________________________________
|
||||
0 |
|
||||
1 |
|
||||
2 - - |
|
||||
3- - - - --- --- - - |
|
||||
4- - - - - ---- -------------------------------------|
|
||||
5 -- -- -- -- |
|
||||
6 |
|
||||
7 |
|
||||
8 |
|
||||
9 |
|
||||
0123456789012345678901234567890123456789012345678901234567890123
|
||||
I (318) view: Plot: Length=128, min=-1.000000, max=1.000000
|
||||
I (328) view: Data min[511] = -149.480377, Data max[0] = 0.000000
|
||||
________________________________________________________________
|
||||
0 -- |
|
||||
1 -- - |
|
||||
2---------- ----- |
|
||||
3 -------- |
|
||||
4 ------------ |
|
||||
5 ---------- |
|
||||
6 ------- |
|
||||
7 --- |
|
||||
8 -- |
|
||||
9 --|
|
||||
0123456789012345678901234567890123456789012345678901234567890123
|
||||
I (408) view: Plot: Length=512, min=-100.000000, max=0.000000
|
||||
I (408) main: IIR for 1024 samples take 17456 cycles
|
||||
I (418) main: End Example.
|
||||
|
||||
```
|
||||
@@ -0,0 +1 @@
|
||||
idf_component_register(SRCS "dsps_iir_main.c")
|
||||
@@ -0,0 +1,111 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "soc/gpio_struct.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/uart.h"
|
||||
#include "soc/uart_struct.h"
|
||||
#include <math.h>
|
||||
|
||||
#include "esp_dsp.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// This example shows how to use iir filters from esp-dsp library
|
||||
|
||||
#define N_SAMPLES 1024
|
||||
int N = N_SAMPLES;
|
||||
// Input test array
|
||||
__attribute__((aligned(16)))
|
||||
float d[N_SAMPLES];
|
||||
// output array
|
||||
__attribute__((aligned(16)))
|
||||
float y[N_SAMPLES];
|
||||
__attribute__((aligned(16)))
|
||||
float y_cf[N_SAMPLES * 2];
|
||||
|
||||
// Function shows result of IIR filter
|
||||
void ShowIIRfilter(float freq, float qFactor)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
float coeffs_lpf[5];
|
||||
float w_lpf[5] = {0, 0};
|
||||
// Calculate iir filter coefficients
|
||||
ret = dsps_biquad_gen_lpf_f32(coeffs_lpf, freq, qFactor);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Operation error = %i", ret);
|
||||
return;
|
||||
}
|
||||
// Process input signal
|
||||
unsigned int start_b = dsp_get_cpu_cycle_count();
|
||||
ret = dsps_biquad_f32(d, y, N, coeffs_lpf, w_lpf);
|
||||
unsigned int end_b = dsp_get_cpu_cycle_count();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Operation error = %i", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
// Show result as a plot
|
||||
ESP_LOGI(TAG, "Impulse response of IIR filter with F=%f, qFactor=%f", freq, qFactor);
|
||||
dsps_view(y, 128, 64, 10, -1, 1, '-');
|
||||
|
||||
// Show result as frequency responce on the plot
|
||||
for (int i = 0 ; i < N ; i++) {
|
||||
y_cf[i * 2 + 0] = y[i];
|
||||
y_cf[i * 2 + 1] = 0;
|
||||
}
|
||||
|
||||
// We making FFT transform
|
||||
dsps_fft2r_fc32_ansi(y_cf, N);
|
||||
// Bit reverse
|
||||
dsps_bit_rev_fc32_ansi(y_cf, N);
|
||||
// Calculating power of spectrum in dB
|
||||
for (int i = 0 ; i < N / 2 ; i++) {
|
||||
y_cf[i] = 10 * log10f((y_cf[i * 2 + 0] * y_cf[i * 2 + 0] + y_cf[i * 2 + 1] * y_cf[i * 2 + 1]) / N);
|
||||
}
|
||||
// Show power spectrum in 64x10 window from -100 to 0 dB from 0..N/2 samples
|
||||
dsps_view(y_cf, N / 2, 64, 10, -100, 0, '-');
|
||||
ESP_LOGI(TAG, "IIR for %i samples take %i cycles", N, end_b - start_b);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
esp_err_t ret;
|
||||
ESP_LOGI(TAG, "Start Example.");
|
||||
// If user don't care about buffer allocation, the defalt
|
||||
// initialization could be used as shown here:
|
||||
ret = dsps_fft2r_init_fc32(NULL, CONFIG_DSP_MAX_FFT_SIZE);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Not possible to initialize FFT. Error = %i", ret);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize input signal
|
||||
// Generate d function as input signal
|
||||
dsps_d_gen_f32(d, N, 0);
|
||||
|
||||
// Show filter with Q factor 1
|
||||
ShowIIRfilter(0.1, 1);
|
||||
// Show filter with Q factor 10
|
||||
ShowIIRfilter(0.1, 10);
|
||||
|
||||
ESP_LOGI(TAG, "End Example.");
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
dependencies:
|
||||
espressif/esp-dsp:
|
||||
override_path: "../../../"
|
||||
version: "*"
|
||||
@@ -0,0 +1 @@
|
||||
CONFIG_PARTITION_TABLE_OFFSET=0x9000
|
||||
Reference in New Issue
Block a user