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,118 @@
// Copyright 2018-2020 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 "dsp_common.h"
#include <math.h>
#include "dsps_dct.h"
#include "dsps_fft2r.h"
esp_err_t dsps_dct_f32_ref(float *data, int N, float *result)
{
float factor = M_PI / N;
for (size_t i = 0; i < N; i++) {
float sum = 0;
for (size_t j = 0; j < N; j++) {
sum += data[j] * cosf((j + 0.5) * i * factor);
}
result[i] = sum;
}
return ESP_OK;
}
esp_err_t dsps_dct_inverce_f32_ref(float *data, int N, float *result)
{
float factor = M_PI / N;
for (size_t i = 0; i < N; i++) {
float sum = data[0] / 2;
for (size_t j = 0; j < N; j++) {
sum += data[j] * cosf(j * (i + 0.5) * factor);
}
result[i] = sum;
}
return ESP_OK;
}
esp_err_t dsps_dct_f32(float *data, int N)
{
esp_err_t ret = ESP_OK;
if (dsps_fft2r_initialized == 0) {
return ESP_ERR_DSP_REINITIALIZED;
}
for (int i = 0; i < N / 2; i++) {
data[(N - 1 - i) * 2] = data[i * 2 + 1];
data[i * 2 + 1] = 0;
data[N + i * 2 + 1] = 0;
}
ret = dsps_fft2r_fc32(data, N);
if (ret != ESP_OK) {
return ret;
}
// // The follows code do the same as this one:
// //
// float factor = M_PI / (N * 2);
// ret = dsps_bit_rev_fc32(data, N);
// for (int i = 0; i < N; i++) {
// float temp = i * factor;
// data[i] = data[i*2] * cosf(temp) + data[i*2 + 1] * sinf(temp);
// }
int table_step = 2;
for (int i = 0; i < N; i++) {
float c = dsps_fft_w_table_fc32[i * 2 * table_step];
float s = dsps_fft_w_table_fc32[i * 2 * table_step + 1];
data[i * 2] = data[i * 2] * c;
data[i * 2 + 1] = data[i * 2 + 1] * s;
}
ret = dsps_bit_rev_fc32(data, N);
if (ret != ESP_OK) {
return ret;
}
for (int i = 0; i < N; i++) {
data[i] = data[i * 2] + data[i * 2 + 1];
}
return ESP_OK;
}
esp_err_t dsps_dct_inv_f32(float *data, int N)
{
esp_err_t ret = ESP_OK;
if (dsps_fft2r_initialized == 0) {
return ESP_ERR_DSP_REINITIALIZED;
}
float factor = M_PI / (N * 2);
data[0] *= 0.5;
for (int i = N - 1; i >= 0; i--) {
float temp = i * factor;
data[i * 2] = data[i] * cosf(temp);
data[i * 2 + 1] = data[i] * -sinf(temp);
}
ret = dsps_fft2r_fc32(data, N);
if (ret != ESP_OK) {
return ret;
}
ret = dsps_bit_rev_fc32(data, N);
if (ret != ESP_OK) {
return ret;
}
for (size_t i = 0; i < N / 2; i++) {
data[i * 2 + 1] = data[(N - 1 - i) * 2];
}
return ESP_OK;
}

View File

@@ -0,0 +1,94 @@
// Copyright 2018-2020 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 "dsp_common.h"
#include <math.h>
#include "dsps_dct.h"
#include "dsps_fft2r.h"
esp_err_t dsps_dctiv_f32(float *data, int ndct)
{
if (dsps_fft2r_initialized == 0) {
return ESP_ERR_DSP_REINITIALIZED;
}
float factor = M_PI / (ndct * 2);
float in1, in2, in3, in4;
for (int i = 0; i < ndct / 4; i++) {
in1 = data[i * 2 + 0];
in2 = data[i * 2 + 1];
in3 = data[ndct - i * 2 - 1];
in4 = data[ndct - i * 2 - 2];
data[i * 2 + 0] = (
in1 * cos(factor * (i * 2 + 0))
+ in3 * cos(factor * ((ndct - i * 2)))
);
data[i * 2 + 1] = (
-in1 * sin(factor * (i * 2))
+ in3 * sin(factor * ((ndct - i * 2)))
);
data[ndct - i * 2 + 0 - 2] = (
in2 * cos(factor * (i * 2 + 1 + 0.5) )
+ in4 * cos(factor * ((ndct - i * 2 - 1) - 0.5) )
);
data[ndct - i * 2 + 1 - 2] = (
in2 * sin(factor * (i * 2 + 1))
+ in4 * sin(-factor * ((ndct - i * 2 - 1)) )
);
}
esp_err_t error = ESP_OK;
error = dsps_fft2r_fc32(data, ndct / 2);
if (error != ESP_OK) {
return error;
}
error = dsps_bit_rev_fc32(data, ndct / 2);
if (error != ESP_OK) {
return error;
}
for (int i = 0; i < ndct / 4; i++) {
in1 = data[2 * i + 0];
in2 = data[2 * i + 1];
in3 = data[ndct - 2 * i - 2];
in4 = data[ndct - 2 * i - 1];
data[i * 2 + 0] = (
in1 * cos(factor * (0 + i * 2))
+ in2 * sin(factor * (0 + i * 2))
);
data[ndct - i * 2 - 1] = (
in1 * cos(factor * (ndct - i * 2))
- in2 * sin(factor * (ndct - i * 2))
);
data[i * 2 + 1] = (
in3 * cos(factor * (2 + i * 2))
- in4 * sin(factor * (2 + i * 2))
);
data[ndct - i * 2 - 2] = (
in3 * cos(factor * (ndct - i * 2 - 2) )
+ in4 * sin(factor * (ndct - i * 2 - 2) )
);
}
return ESP_OK;
}

View File

@@ -0,0 +1,98 @@
// Copyright 2018-2020 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 "dsp_common.h"
#include <math.h>
#include "dsps_dct.h"
#include "dsps_fft2r.h"
esp_err_t dsps_dstiv_f32(float *data, int ndst)
{
if (dsps_fft2r_initialized == 0) {
return ESP_ERR_DSP_REINITIALIZED;
}
float in1, in2, in3, in4;
float factor = M_PI / (ndst);
for (int i = 0; i < ndst / 4; i++) {
in1 = data[2 * i + 0];
in2 = data[2 * i + 1];
in3 = data[ndst - 2 * i - 2];
in4 = data[ndst - 2 * i - 1];
data[i * 2 + 1] = (
in1 * cos(factor * (i + 0))
- in4 * sin(factor * ((ndst - i - 1)))
);
data[i * 2 + 0] = (
in1 * sin(factor * (i))
- in4 * cos(factor * ((ndst - i - 1)))
);
data[ndst - i * 2 - 2] = (
-in3 * cos(factor * (ndst - i - 1))
+ in2 * sin(factor * (ndst - i - 1))
);
data[ndst - i * 2 - 1] = (
+in3 * sin(factor * (i + 1))
- in2 * cos(-factor * (i + 1))
);
}
esp_err_t error = ESP_OK;
error = dsps_fft2r_fc32(data, ndst / 2);
if (error != ESP_OK) {
return error;
}
error = dsps_bit_rev_fc32(data, ndst / 2);
if (error != ESP_OK) {
return error;
}
for (int i = 0; i < ndst / 4; i++) {
in1 = data[2 * i + 0];
in2 = data[2 * i + 1];
in3 = data[ndst - 2 * i - 2];
in4 = data[ndst - 2 * i - 1];
data[i * 2 + 0] = (
in1 * cos(factor * (0 + i))
+ in2 * sin(factor * (0 + i))
);
data[ndst - i * 2 - 2 + 1] = (
-in1 * cos(factor * (ndst / 2 - i))
+ in2 * sin(factor * (ndst / 2 - i))
);
data[i * 2 + 1] = (
-in3 * cos(factor * (1 + i))
+ in4 * sin(factor * (1 + i))
);
data[ndst - i * 2 - 2 + 0] = (
+in3 * cos(factor * (ndst / 2 - i - 1))
+ in4 * sin(factor * (ndst / 2 - i - 1))
);
}
return ESP_OK;
}

View File

@@ -0,0 +1,137 @@
// Copyright 2018-2020 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.
#ifndef _dsps_dct_H_
#define _dsps_dct_H_
#include "dsp_err.h"
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**@{*/
/**
* @brief DCT of radix 2, unscaled
*
* Discrete Cosine Transform type II of radix 2, unscaled
* Function is FFT based
* The extension (_ansi) use ANSI C and could be compiled and run on any platform.
* The extension (_ae32) is optimized for ESP32 chip.
*
* @param[inout] data: input/output array with size of N*2. An elements located: Re[0],Re[1], , ... Re[N-1], any data... up to N*2
* result of DCT will be stored to this array from 0...N-1.
* Size of data array must be N*2!!!
* @param[in] N: Size of DCT transform. Size of data array must be N*2!!!
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_dct_f32(float *data, int N);
/**@}*/
/**@{*/
/**
* @brief DCT of radix 2, type IV, unscaled
*
* Discrete Cosine Transform type IV of radix 2, unscaled
* Function is FFT based
* The extension (_ansi) use ANSI C and could be compiled and run on any platform.
* The extension (_ae32) is optimized for ESP32 chip.
*
* @param[inout] data: input/output array with size of N. An elements located: Re[0],Re[1], , ... Re[N-1]
* result of DST will be stored to this array from 0...N-1.
* Size of data array must be N
* @param[in] N: Size of DCT transform. Size of data array must be N
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_dctiv_f32(float *data, int N);
/**@}*/
/**@{*/
/**
* @brief DST of radix 2, type IV, unscaled
*
* Discrete Sine Transform type IV of radix 2, unscaled
* Function is FFT based
* The extension (_ansi) use ANSI C and could be compiled and run on any platform.
* The extension (_ae32) is optimized for ESP32 chip.
*
* @param[inout] data: input/output array with size of N*2. An elements located: Re[0],Re[1], , ... Re[N-1]
* result of DST will be stored to this array from 0...N-1.
* Size of data array must be N
* @param[in] N: Size of DST transform. Size of data array must be N
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_dstiv_f32(float *data, int N);
/**@}*/
/**@{*/
/**
* @brief Inverce DCT of radix 2
*
* Inverce Discrete Cosine Transform type II of radix 2, unscaled
* Function is FFT based
* The extension (_ansi) use ANSI C and could be compiled and run on any platform.
* The extension (_ae32) is optimized for ESP32 chip.
*
* @param[inout] data: input/output array with size of N*2. An elements located: Re[0],Re[1], , ... Re[N-1], any data... up to N*2
* result of DCT will be stored to this array from 0...N-1.
* Size of data array must be N*2!!!
* @param[in] N: Size of DCT transform. Size of data array must be N*2!!!
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_dct_inv_f32(float *data, int N);
/**@}*/
/**@{*/
/**
* @brief DCTs
*
* Direct DCT type II and Inverce DCT type III, unscaled
* These functions used as a reference for general purpose. These functions are not optimyzed!
* The extension (_ansi) use ANSI C and could be compiled and run on any platform.
* The extension (_ae32) is optimized for ESP32 chip.
*
* @param[in] data: input/output array with size of N. An elements located: Re[0],Re[1], , ... Re[N-1]
* @param[in] N: Size of DCT transform. Size of data array must be N*2!!!
* @param[out] result: output result array with size of N.
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_dct_f32_ref(float *data, int N, float *result);
esp_err_t dsps_dct_inverce_f32_ref(float *data, int N, float *result);
/**@}*/
#ifdef __cplusplus
}
#endif
#endif // _dsps_dct_H_

View File

@@ -0,0 +1,166 @@
// Copyright 2018-2020 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 <string.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsps_view.h"
#include "dsps_dct.h"
#include "dsps_fft2r.h"
#include "dsp_tests.h"
#include <malloc.h>
static const char *TAG = "dsps_dct";
TEST_CASE("dsps_dct_f32 functionality", "[dsps]")
{
float *data = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data);
float *data_ref = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data_ref);
float *data_fft = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data_fft);
int N = 64;
int check_bin = 4;
for (int i = 0 ; i < N ; i++) {
data[i] = 2 * sinf(M_PI / N * check_bin * 2 * i);
data_ref[i] = data[i];
data_fft[i] = data[i];
data[i + N] = 0;
data_ref[i + N] = 0;
data_fft[i + N] = 0;
}
dsps_dct_f32_ref(data, N, &data[N]);
dsps_view(&data[N], 32, 32, 10, -2, 2, '.');
dsps_dct_inverce_f32_ref(&data[N], N, data);
dsps_view(&data[0], 32, 32, 10, -2, 2, '.');
for (int i = 0; i < N; i++) {
ESP_LOGD(TAG, "DCT data[%i] = %2.3f\n", i, data[N + i]);
}
float abs_tol = 1e-5;
for (int i = 1; i < N; i++) {
ESP_LOGD(TAG, "data[%i] = %f, ref_data = %f\n", i, data[i], data_ref[i]*N / 2);
float error = fabs(data[i] - data_ref[i] * N / 2) / (N / 2);
if (error > abs_tol) {
ESP_LOGE(TAG, "data[%i] = %f, ref_data = %f, error= %f\n", i, data[i], data_ref[i]*N / 2, error);
TEST_ASSERT_MESSAGE (false, "Result out of range!\n");
}
}
free(data);
free(data_ref);
free(data_fft);
}
TEST_CASE("dsps_dct_f32 functionality Fast DCT", "[dsps]")
{
esp_err_t ret = dsps_fft2r_init_fc32(NULL, CONFIG_DSP_MAX_FFT_SIZE);
TEST_ESP_OK(ret);
float *data = (float *)memalign(16, sizeof(float) * 1024 * 2);
TEST_ASSERT_NOT_NULL(data);
float *data_ref = (float *)memalign(16, sizeof(float) * 1024 * 2);
TEST_ASSERT_NOT_NULL(data_ref);
float *data_fft = (float *)memalign(16, sizeof(float) * 1024 * 2);
TEST_ASSERT_NOT_NULL(data_fft);
int N = 64;
int check_bin = 4;
for (int i = 0 ; i < N ; i++) {
data[i] = 2 * sin(M_PI / N * check_bin * 2 * i);
data_ref[i] = data[i];
data_fft[i] = data[i];
data[i + N] = 0;
data_ref[i + N] = 0;
data_fft[i + N] = 0;
}
dsps_dct_f32_ref(data, N, &data[N]);
ret = dsps_dct_f32(data_fft, N);
TEST_ESP_OK(ret);
float abs_tol = 1e-5;
for (int i = 0; i < N; i++) {
ESP_LOGD(TAG, "DCT data[%i] = %2.3f, data_fft = %2.3f\n", i, data[N + i], data_fft[i]);
float error = fabs(data[N + i] - data_fft[i]) / (N / 2);
if (error > abs_tol) {
ESP_LOGE(TAG, "DCT data[%i] = %f, data_fft = %f, error = %f\n", i, data[N + i], data_fft[i], error);
TEST_ASSERT_MESSAGE (false, "Result out of range!\n");
}
}
dsps_dct_inv_f32(data_fft, N);
for (int i = 0; i < N; i++) {
ESP_LOGD(TAG, "IDCT data[%i] = %2.3f, data_fft = %2.3f\n", i, data[i], data_fft[i] / N * 2);
float error = fabs(data[i] - data_fft[i] / N * 2) / (N / 2);
if (error > abs_tol) {
ESP_LOGE(TAG, "IDCT data[%i] = %f, data_fft = %f, error = %f\n", i, data[i], data_fft[i] / N * 2, error);
TEST_ASSERT_MESSAGE (false, "Result out of range!\n");
}
}
dsps_fft2r_deinit_fc32();
free(data);
free(data_ref);
free(data_fft);
}
TEST_CASE("dsps_dct_f32 benchmark", "[dsps]")
{
esp_err_t ret = dsps_fft2r_init_fc32(NULL, CONFIG_DSP_MAX_FFT_SIZE);
TEST_ESP_OK(ret);
float *data = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data);
float *data_ref = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data_ref);
float *data_fft = calloc(1024 * 2, sizeof(float));
TEST_ASSERT_NOT_NULL(data_fft);
int N = 64;
int check_bin = 4;
for (int i = 0 ; i < N ; i++) {
data[i] = 2 * sin(M_PI / N * check_bin * 2 * i);
data[i + N] = 0;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
ret = dsps_dct_f32(data, N);
unsigned int end_b = dsp_get_cpu_cycle_count();
TEST_ESP_OK(ret);
float total_b = end_b - start_b;
float cycles = total_b;
ESP_LOGI(TAG, "Benchmark dsps_dct_f32 - %6i cycles for %6i DCT points FFT.", (int)cycles, N);
dsps_fft2r_deinit_fc32();
free(data);
free(data_ref);
free(data_fft);
}