add some code

This commit is contained in:
2025-09-05 13:25:11 +08:00
parent 9ff0a99e7a
commit 3cf1229a85
8911 changed files with 2535396 additions and 0 deletions

View File

@@ -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(fft4real)

View File

@@ -0,0 +1,91 @@
# FFT 4 Real Input Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example demonstrates how to use FFT functionality from esp-dsp library. Example does the following steps:
1. Initialize the library
2. Initialize input signals with 1024 samples: one 0 dB, second with -20 dB
3. Calculate FFT Radix-2 for 1024 complex samples
4. Calculate FFT Radix-4 for 1024 complex samples
5. Apply bit reverse operation for output complex vectors
6. Show results on the plots
7. Show execution time of FFTs
## 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 (344) main: Start Example.
W (424) main: Signal x1
I (424) view: Data min[673] = -103.113297, Data max[328] = 20.490950
________________________________________________________________
0 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | | |
8 | | |
9||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||
0123456789012345678901234567890123456789012345678901234567890123
I (494) view: Plot: Length=1024, min=-60.000000, max=40.000000
W (504) main: Signal x2
I (504) view: Data min[582] = -103.113297, Data max[328] = 20.490950
________________________________________________________________
0 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | | |
8 | | |
9||||||||||||||||||| |||||||||||||||||||||||||||||||||||||||||||
0123456789012345678901234567890123456789012345678901234567890123
I (584) view: Plot: Length=1024, min=-60.000000, max=40.000000
W (593) main: Difference between signals x1 and x2 on one plot
I (594) view: Data min[0] = 0.000000, Data max[392] = 0.313019
________________________________________________________________
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8----------------------------------------------------------------|
9 |
0123456789012345678901234567890123456789012345678901234567890123
I (674) view: Plot: Length=1024, min=0.000000, max=40.000000
I (674) main: FFT Radix 2 for 1024 complex points take 168652 cycles
I (684) main: FFT Radix 4 for 1024 complex points take 104665 cycles
I (694) main: End Example.
```

View File

@@ -0,0 +1 @@
idf_component_register(SRCS "dsps_fft4real_main.c")

View File

@@ -0,0 +1,115 @@
// 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 FFT from esp-dsp library
#define N_SAMPLES 2048 // Amount of real input samples
int N = N_SAMPLES;
// Input test array
__attribute__((aligned(16)))
float x1[N_SAMPLES];
__attribute__((aligned(16)))
float x2[N_SAMPLES];
// Window coefficients
__attribute__((aligned(16)))
float wind[N_SAMPLES];
// Pointers to result arrays
float *y1_cf = &x1[0];
float *y2_cf = &x2[0];
// diff of y1 and y2
__attribute__((aligned(16)))
float diff_y[N_SAMPLES / 2];
void app_main()
{
esp_err_t ret;
ESP_LOGI(TAG, "Start Example.");
ret = dsps_fft2r_init_fc32(NULL, N >> 1);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Not possible to initialize FFT2R. Error = %i", ret);
return;
}
ret = dsps_fft4r_init_fc32(NULL, N >> 1);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Not possible to initialize FFT4R. Error = %i", ret);
return;
}
// Generate hann window
dsps_wind_hann_f32(wind, N);
// Generate input signal for x1 A=1 , F=0.1
dsps_tone_gen_f32(x1, N, 1.0, 0.16, 0);
// Convert two input vectors to one complex vector
for (int i = 0 ; i < N ; i++) {
x1[i] = x1[i] * wind[i];
x2[i] = x1[i];
}
// FFT Radix-2
unsigned int start_r2 = dsp_get_cpu_cycle_count();
dsps_fft2r_fc32(x1, N >> 1);
// Bit reverse
dsps_bit_rev2r_fc32(x1, N >> 1);
// Convert one complex vector with length N/2 to one real spectrum vector with length N/2
dsps_cplx2real_fc32(x1, N >> 1);
unsigned int end_r2 = dsp_get_cpu_cycle_count();
// FFT Radix-4
unsigned int start_r4 = dsp_get_cpu_cycle_count();
dsps_fft4r_fc32(x2, N >> 1);
// Bit reverse
dsps_bit_rev4r_fc32(x2, N >> 1);
// Convert one complex vector with length N/2 to one real spectrum vector with length N/2
dsps_cplx2real_fc32(x2, N >> 1);
unsigned int end_r4 = dsp_get_cpu_cycle_count();
for (int i = 0 ; i < N / 2 ; i++) {
x1[i] = 10 * log10f((x1[i * 2 + 0] * x1[i * 2 + 0] + x1[i * 2 + 1] * x1[i * 2 + 1] + 0.0000001) / N);
x2[i] = 10 * log10f((x2[i * 2 + 0] * x2[i * 2 + 0] + x2[i * 2 + 1] * x2[i * 2 + 1] + 0.0000001) / N);
// Simple way to show two power spectrums as one plot
diff_y[i] = fabs(x1[i] - x2[i]);
}
// Show power spectrum in 64x10 window from -100 to 0 dB from 0..N/4 samples
ESP_LOGW(TAG, "Signal x1");
dsps_view(x1, N / 2, 64, 10, -60, 40, '|');
ESP_LOGW(TAG, "Signal x2");
dsps_view(x2, N / 2, 64, 10, -60, 40, '|');
ESP_LOGW(TAG, "Difference between signals x1 and x2 on one plot");
dsps_view(diff_y, N / 2, 64, 10, 0, 40, '-');
ESP_LOGI(TAG, "FFT Radix 2 for %i complex points take %i cycles", N / 2, end_r2 - start_r2);
ESP_LOGI(TAG, "FFT Radix 4 for %i complex points take %i cycles", N / 2, end_r4 - start_r4);
ESP_LOGI(TAG, "End Example.");
}

View File

@@ -0,0 +1,4 @@
dependencies:
espressif/esp-dsp:
override_path: "../../../"
version: "*"

View File

@@ -0,0 +1,9 @@
[mapping:dsp]
archive: libdsp.a
entries:
* (noflash)
[mapping:esp-dsp]
archive: libesp-dsp.a
entries:
* (noflash)

View File

@@ -0,0 +1 @@
CONFIG_PARTITION_TABLE_OFFSET=0x9000