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,184 @@
// Copyright 2024 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 "dspi_conv.h"
#include "esp_log.h"
esp_err_t dspi_conv_f32_ansi(const image2d_t *in_image, const image2d_t *filter, image2d_t *out_image)
{
out_image->size_x = in_image->size_x;
out_image->size_y = in_image->size_y;
float *i_data = (float *)in_image->data;
float *out_data = (float *)out_image->data;
int rest_x = (filter->size_x - 1) >> 1;
int rest_y = (filter->size_y - 1) >> 1;
int i_pos = 0;
int i_step = in_image->stride_x * in_image->step_y;
int f_step = filter->stride_x * filter->step_y;
// Up side of image
for (int y = 0 ; y < rest_y; y++ ) {
int i_pos_y = i_pos;
for (int x = 0 ; x < rest_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = rest_y - y ; m < filter->size_y ; m++) {
for (int n = rest_x - x ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
for (int x = rest_x ; x < in_image->size_x - filter->size_x / 2; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = rest_y - y ; m < filter->size_y ; m++) {
for (int n = 0 ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
for (int x = in_image->size_x - filter->size_x / 2 - 1; x < in_image->size_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = rest_y - y ; m < filter->size_y ; m++) {
for (int n = 0 ; n < filter->size_x - (x - in_image->size_x + filter->size_x / 2 + 1); n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
i_pos += in_image->stride_x * in_image->step_y;
}
// Middle side of image
i_pos = 0;
for (int y = rest_y ; y < in_image->size_y - filter->size_y / 2; y++ ) {
int i_pos_y = i_pos;
for (int x = 0 ; x < rest_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y ; m++) {
for (int n = rest_x - x ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
for (int x = in_image->size_x - filter->size_x / 2 - 1; x < in_image->size_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y ; m++) {
for (int n = 0 ; n < filter->size_x - (x - in_image->size_x + filter->size_x / 2 + 1); n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
i_pos += in_image->stride_x * in_image->step_y;
}
// Down side of image
i_pos = 0;
for (int y = in_image->size_y - filter->size_y / 2 ; y < in_image->size_y; y++ ) {
int i_pos_y = i_pos;
for (int x = 0 ; x < rest_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y - (y - in_image->size_y + filter->size_y / 2 + 1); m++) {
for (int n = rest_x - x ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
for (int x = rest_x ; x < in_image->size_x - filter->size_x / 2; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y - (y - in_image->size_y + filter->size_y / 2 + 1); m++) {
for (int n = 0 ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
for (int x = in_image->size_x - filter->size_x / 2 ; x < in_image->size_x; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y - (y - in_image->size_y + filter->size_y / 2 + 1); m++) {
for (int n = 0 ; n < filter->size_x - (x - in_image->size_x + filter->size_x / 2 + 1); n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
i_pos += in_image->stride_x * in_image->step_y;
}
// Main image block
i_pos = 0;
for (int y = rest_y ; y < in_image->size_y - filter->size_y / 2; y++ ) {
int i_pos_y = i_pos;
for (int x = rest_x ; x < in_image->size_x - filter->size_x / 2; x++) {
int i_pos_x = i_pos_y;
float acc = 0;
float *f_data = (float *)filter->data;
for (int m = 0 ; m < filter->size_y ; m++) {
for (int n = 0 ; n < filter->size_x ; n++) {
acc += i_data[i_pos_x + n * in_image->step_x] * f_data[filter->step_x * n];
}
f_data += f_step;
i_pos_x += i_step;
}
i_pos_y += in_image->step_x;
out_data[x * out_image->step_x + y * out_image->stride_x * out_image->step_y] = acc;
}
i_pos += in_image->stride_x * in_image->step_y;
}
return ESP_OK;
}

View File

@@ -0,0 +1,144 @@
// 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 "dsps_conv_platform.h"
#if (dsps_ccorr_f32_ae32_enabled == 1)
#include "dsps_conv_f32_m_ae32.S"
// This is dot product function for ESP32 processor.
.text
.align 4
.global dsps_ccorr_f32_ae32
.type dsps_ccorr_f32_ae32,@function
// The function implements the C code from dsps_ccorr_f32_ansi:
//esp_err_t dsps_ccorr_f32_ansi(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *corrout);
//
dsps_ccorr_f32_ae32:
// Signal - a2
// siglen - a3
// Kernel - a4
// kernlen - a5
// corrout - a6
//
// a11 - loop length
entry a1, 16
// Array increment for floating point data should be 4
sub a10, a3, a5
bgez a10, dsps_ccorr_positive
addi a10, a2, 0
addi a2, a4, 0
addi a4, a10, 0
addi a10, a3, 0
addi a3, a5, 0
addi a5, a10, 0
dsps_ccorr_positive:
movi.n a8, 4
addi a11, a5, 0 // lkern - loop counter
movi.n a14, 0
addi a9, a14, 1
movi.n a7, 4
movi.n a8, -4
mull a13, a5, a7 // a13 - kernlen*4
add a13, a13, a4 // a13 - Kernel[kernlen]
addi a13, a13, -4 // a13 - Kernel[kernlen - 1]
ccorr_loop1:
// Clear initial state of the result register
addi a10, a13, 0 // a10 - Kernel
addi a12, a2, 0 // a12 - Signal
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[0]
// a10 - kern[n];
// a9 - n+1
// a7 - 4,
// a8 - -4,
conv_f32_ae32 a12, a10, a9, a7, a7, loop1
addi a9, a9, 1 // (n+1)++
addi a13, a13, -4 // kern[n] - a4--
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a11, a11, -1
bnez a11, ccorr_loop1
// a11 - loop counter = siglen - kernlen - 1
addi a9, a2, 4 // sig[1] - sig[kmin]
addi a13, a5, 0
// skip loop if 0
sub a11, a3, a5 // a11 - loop counter
beqz a11, skip_ccorr_loop2
ccorr_loop2:
// Clear initial state of the result register
addi a12, a9, 0 // a12 - Signal[kmin]
addi a10, a4, 0 // a10 - Kernel
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[kmin]
// a10 - kern[0];
// a11 - kernlen
// a7 - 4,
conv_f32_ae32 a12, a10, a13, a7, a7, loop2
addi a9, a9, 4 // in1++
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a11, a11, -1
bnez a11, ccorr_loop2
skip_ccorr_loop2:
// a9 - the same
addi a11, a5, -1
addi a13, a5, -1
ccorr_loop3:
// Clear initial state of the result register
addi a12, a9, 0 // a12 - Signal[kmin]
addi a10, a4, 0 // a10 - Kernel
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[kmin]
// a10 - kern[n - kmin];
// a11 - length
// a7 - 4,
// a8 - -4,
conv_f32_ae32 a12, a10, a11, a7, a7, loop3
addi a9, a9, 4 // n++
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a11, a11, -1
bnez a11, ccorr_loop3
skip_ccorr_loop3:
movi.n a2, 0 // return status ESP_OK
retw.n
#endif // dsps_ccorr_f32_ae32_enabled

View File

@@ -0,0 +1,81 @@
// 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 "dsps_conv.h"
#include "esp_log.h"
static const char *TAG = "dsps_conv";
esp_err_t dsps_ccorr_f32_ansi(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *corrvout)
{
if (NULL == Signal) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == Kernel) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == corrvout) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
float *sig = (float *)Signal;
float *kern = (float *)Kernel;
int lsig = siglen;
int lkern = kernlen;
if (siglen < kernlen) {
sig = (float *)Kernel;
kern = (float *)Signal;
lsig = kernlen;
lkern = siglen;
}
for (int n = 0; n < lkern; n++) {
int k;
int kmin = lkern - 1 - n;
corrvout[n] = 0;
for (k = 0; k <= n; k++) {
corrvout[n] += sig[k] * kern[kmin + k];
}
ESP_LOGV(TAG, "L1 k = %i, n = %i , kmin= %i, kmax= %i", 0, n, kmin, kmin + n);
}
for (int n = lkern; n < lsig; n++) {
int kmin, kmax, k;
corrvout[n] = 0;
kmin = n - lkern + 1;
kmax = n;
for (k = kmin; k <= kmax; k++) {
corrvout[n] += sig[k] * kern[k - kmin];
}
ESP_LOGV(TAG, "L2 n=%i, kmin = %i, kmax = %i , k-kmin = %i", n, kmin, kmax, 0);
}
for (int n = lsig; n < lsig + lkern - 1; n++) {
int kmin, kmax, k;
corrvout[n] = 0;
kmin = n - lkern + 1;
kmax = lsig - 1;
for (k = kmin; k <= kmax; k++) {
corrvout[n] += sig[k] * kern[k - kmin];
}
ESP_LOGV(TAG, "L3 n=%i, kmin = %i, kmax = %i , k - kmin = %i", n, kmin, kmax, kmax - kmin);
}
return ESP_OK;
}

View File

@@ -0,0 +1,147 @@
// 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 "dsps_conv_platform.h"
#if (dsps_conv_f32_ae32_enabled == 1)
#include "dsps_conv_f32_m_ae32.S"
// This is dot product function for ESP32 processor.
.text
.align 4
.global dsps_conv_f32_ae32
.type dsps_conv_f32_ae32,@function
// The function implements the C code from dsps_conv_f32_ansi:
//esp_err_t dsps_conv_f32_ansi(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout);
//
dsps_conv_f32_ae32:
// Signal - a2
// siglen - a3
// Kernel - a4
// kernlen - a5
// convout - a6
//
// a11 - loop length
entry a1, 16
// Array increment for floating point data should be 4
sub a10, a3, a5
bgez a10, dsps_conv_positive
addi a10, a2, 0
addi a2, a4, 0
addi a4, a10, 0
addi a10, a3, 0
addi a3, a5, 0
addi a5, a10, 0
dsps_conv_positive:
movi.n a8, 4
addi a11, a5, 0 // lkern - loop counter
movi.n a14, 0
addi a9, a14, 1
movi.n a7, 4
movi.n a8, -4
conv_loop1:
// Clear initial state of the result register
addi a10, a4, 0 // a10 - Kernel
addi a12, a2, 0 // a12 - Signal
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[0]
// a10 - kern[n];
// a9 - n+1
// a7 - 4,
// a8 - -4,
conv_f32_ae32 a12, a10, a9, a7, a8, loop1
addi a9, a9, 1 // (n+1)++
addi a4, a4, 4 // kern[n] - a4++
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a11, a11, -1
bnez a11, conv_loop1
// a11 - loop counter = siglen - kernlen - 1
addi a9, a2, 0 // sig[1] - sig[kmin]
addi a13, a5, 0
// skip loop if 0
sub a11, a3, a5 // a11 - loop counter
beqz a11, skip_conv_loop2
conv_loop2:
// Clear initial state of the result register
addi a12, a9, 4 // a12 - Signal[kmin]
addi a10, a4, -4 // a10 - Kernel
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[kmin]
// a10 - kern[n - kmin];
// a11 - length
// a7 - 4,
// a8 - -4,
conv_f32_ae32 a12, a10, a13, a7, a8, loop2
addi a9, a9, 4 // (n+1)++
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a11, a11, -1
bnez a11, conv_loop2
skip_conv_loop2:
// sub a11, a3, a5 // a11 - loop counter
// beqz a11, skip_conv_loop3
// a9 - the same
addi a11, a5, -1
addi a13, a5, -1
// beqz a11, skip_conv_loop3
conv_loop3:
// Clear initial state of the result register
addi a12, a9, 4 // a12 - Signal[kmin]
addi a10, a4, -4 // a10 - Kernel
wfr f1, a14 // clear output: convout[n] = 0;
// a12 - sig[kmin]
// a10 - kern[n - kmin];
// a11 - length
// a7 - 4,
// a8 - -4,
conv_f32_ae32 a12, a10, a13, a7, a8, loop3
addi a9, a9, 4 // (n+1)++
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // convout++ - increment output pointer
addi a13, a13, -1
addi a11, a11, -1
bnez a11, conv_loop3
skip_conv_loop3:
movi.n a2, 0 // return status ESP_OK
retw.n
#endif // dsps_conv_f32_ae32_enabled

View File

@@ -0,0 +1,81 @@
// 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 "dsps_conv.h"
#include "esp_log.h"
static const char *TAG = "dsps_conv";
esp_err_t dsps_conv_f32_ansi(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout)
{
if (NULL == Signal) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == Kernel) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == convout) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
float *sig = (float *)Signal;
float *kern = (float *)Kernel;
int lsig = siglen;
int lkern = kernlen;
if (siglen < kernlen) {
sig = (float *)Kernel;
kern = (float *)Signal;
lsig = kernlen;
lkern = siglen;
}
for (int n = 0; n < lkern; n++) {
size_t k;
convout[n] = 0;
for (k = 0; k <= n; k++) {
convout[n] += sig[k] * kern[n - k];
}
ESP_LOGV(TAG, "L1 kmin = %i, kmax = %i , n-kmin = %i", 0, n, n);
}
for (int n = lkern; n < lsig; n++) {
int kmin, kmax, k;
convout[n] = 0;
kmin = n - lkern + 1;
kmax = n;
ESP_LOGV(TAG, "L2 n=%i, kmin = %i, kmax = %i , n-kmin = %i", n, kmin, kmax, n - kmin);
for (k = kmin; k <= kmax; k++) {
convout[n] += sig[k] * kern[n - k];
}
}
for (int n = lsig; n < lsig + lkern - 1; n++) {
int kmin, kmax, k;
convout[n] = 0;
kmin = n - lkern + 1;
kmax = lsig - 1;
for (k = kmin; k <= kmax; k++) {
convout[n] += sig[k] * kern[n - k];
}
ESP_LOGV(TAG, "L3 n=%i, kmin = %i, kmax = %i , n-kmin = %i", n, kmin, kmax, n - kmin);
}
return ESP_OK;
}

View File

@@ -0,0 +1,39 @@
// 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.
.macro conv_f32_ae32 x1 x2 count step1 step2 name
// This macro calculates floating point dot product for count float samples
// x1, x2 - input arrays
// count - amount of samples
// step1 - start step
//,step2 - A register for array step increment. (should be divided by 4)
// f1 - contains initial value
//
// result in f1
//
// Macros body:
// f1 += x1[]*x2[]; i: 0..counter-1
// affected: f0, f1, f2
// Example: conv_f32_ae32 a2 a3 a5 a8 a9
// a8 == 4, step is 4 bytes
// a5 == 32, length of array is 32
//
lsxp f0, \x2, \step2
loopnez \count, loop_mac_end_m_ae32\name
lsxp f2, \x1, \step1
madd.s f1, f2, f0
lsxp f0, \x2, \step2
loop_mac_end_m_ae32\name:
.endm

View File

@@ -0,0 +1,77 @@
// 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 "dsps_conv_platform.h"
#if (dsps_corr_f32_ae32_enabled == 1)
#include "dsps_dotprod_f32_m_ae32.S"
// This is dot product function for ESP32 processor.
.text
.align 4
.global dsps_corr_f32_ae32
.type dsps_corr_f32_ae32,@function
// The function implements the following C code:
//esp_err_t dsps_corr_f32_ansi(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *dest)
//{
// for (size_t n = 0; n < (siglen - patlen); n++) {
// float k_corr = 0;
// for (size_t m = 0; m < patlen; m++) {
// k_corr += Signal[n + m] * Pattern[m];
// }
// dest[n] = k_corr;
// }
// return ESP_OK;
//}
dsps_corr_f32_ae32:
// Signal - a2
// siglen - a3
// Pattern - a4
// patlen - a5
// dest - a6
// a11 - loop length
entry a1, 16
// Array increment for floating point data should be 4
movi.n a8, 4
movi.n a13, 4
sub a11, a3, a5 // a11 = loop length
addi a11, a11, 1
addi a12, a2, 0 // move input pointer to the a12
movi.n a9, 0
movi.n a14, 0
corr_loop:
// Clear initial state of the result register
addi a10, a4, 0 // a10 - pattern
movi.n a9, 0 // clear a9
wfr f1, a9 // clrar f1
// a12 - input1
// a10 - input2
// a5 - length
// a8 - 4, step in arrays
// a9 - 0
dotprod_f32_ae32 a12, a10, a5, a9, a8;
ssi f1, a6, 0 // Store result from f1 to memory at a6
addi a6, a6, 4 // y++ - increment output pointer
addi a12, a12, 4 // Signal++
addi a11, a11, -1
bnez a11, corr_loop
movi.n a2, 0 // return status ESP_OK
retw.n
#endif // dsps_corr_f32_ae32_enabled

View File

@@ -0,0 +1,40 @@
// 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 "dsps_corr.h"
esp_err_t dsps_corr_f32_ansi(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *dest)
{
if (NULL == Signal) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == Pattern) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == dest) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (siglen < patlen) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
for (size_t n = 0; n <= (siglen - patlen); n++) {
float k_corr = 0;
for (size_t m = 0; m < patlen; m++) {
k_corr += Signal[n + m] * Pattern[m];
}
dest[n] = k_corr;
}
return ESP_OK;
}

View File

@@ -0,0 +1,55 @@
// Copyright 2024 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 _dspi_conv_H_
#define _dspi_conv_H_
#include "dsp_err.h"
#include "dsps_conv_platform.h"
#include "dsp_types.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**@{*/
/**
* @brief 2D Convolution
*
* The function convolve Signal image with Kernel (filter) image.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @param[in] in_image: input image
* @param[in] filter: input array with convolution kernel
* @param[out] out_image: output image. The stride and step parameters must be set.
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dspi_conv_f32_ansi(const image2d_t *in_image, const image2d_t *filter, image2d_t *out_image);
/**@}*/
#ifdef __cplusplus
}
#endif
#ifdef CONFIG_DSP_OPTIMIZED
#define dspi_conv_f32 dspi_conv_f32_ansi
#else
#define dspi_conv_f32 dspi_conv_f32_ansi
#endif // CONFIG_DSP_OPTIMIZED
#endif // _dspi_conv_H_

View File

@@ -0,0 +1,63 @@
// 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_ccorr_H_
#define _dsps_ccorr_H_
#include "dsp_err.h"
#include "dsps_conv_platform.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**@{*/
/**
* @brief Cross correlation
*
* The function make cross correlate between two ignals.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @param[in] Signal1: input array with input 1 signal values
* @param[in] siglen1: length of the input 1 signal array
* @param[in] Signal2: input array with input 2 signal values
* @param[in] siglen2: length of the input signal array
* @param corrout: output array with result of cross correlation. The size of dest array must be (siglen1 + siglen2 - 1) !!!
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library (one of the input array are NULL, or if (siglen < patlen))
*/
esp_err_t dsps_ccorr_f32_ansi(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *corrout);
esp_err_t dsps_ccorr_f32_ae32(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *corrout);
/**}@*/
#ifdef __cplusplus
}
#endif
#ifdef CONFIG_DSP_OPTIMIZED
#if (dsps_ccorr_f32_ae32_enabled == 1)
#define dsps_ccorr_f32 dsps_ccorr_f32_ae32
#else
#define dsps_ccorr_f32 dsps_ccorr_f32_ansi
#endif // dsps_ccorr_f32_ae32_enabled
#else
#define dsps_ccorr_f32 dsps_ccorr_f32_ansi
#endif
#endif // _dsps_conv_H_

View File

@@ -0,0 +1,65 @@
// 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_conv_H_
#define _dsps_conv_H_
#include "dsp_err.h"
#include "dsps_conv_platform.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**@{*/
/**
* @brief Convolution
*
* The function convolve Signal array with Kernel array.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @param[in] Signal: input array with signal
* @param[in] siglen: length of the input signal
* @param[in] Kernel: input array with convolution kernel
* @param[in] kernlen: length of the Kernel array
* @param convout: output array with convolution result length of (siglen + Kernel -1)
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library
*/
esp_err_t dsps_conv_f32_ae32(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout);
esp_err_t dsps_conv_f32_ansi(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout);
/**@}*/
#ifdef __cplusplus
}
#endif
#ifdef CONFIG_DSP_OPTIMIZED
#if (dsps_conv_f32_ae32_enabled == 1)
#define dsps_conv_f32 dsps_conv_f32_ae32
#else
#define dsps_conv_f32 dsps_conv_f32_ansi
#endif // dsps_conv_f32_ae32_enabled
#else
#define dsps_conv_f32 dsps_conv_f32_ansi
#endif
#endif // _dsps_conv_H_

View File

@@ -0,0 +1,20 @@
#ifndef _dsps_conv_platform_H_
#define _dsps_conv_platform_H_
#include "sdkconfig.h"
#ifdef __XTENSA__
#include <xtensa/config/core-isa.h>
#include <xtensa/config/core-matmap.h>
#if ((XCHAL_HAVE_FP == 1) && (XCHAL_HAVE_LOOPS == 1))
#define dsps_conv_f32_ae32_enabled 1
#define dsps_ccorr_f32_ae32_enabled 1
#define dsps_corr_f32_ae32_enabled 1
#endif
#endif // __XTENSA__
#endif // _dsps_conv_platform_H_

View File

@@ -0,0 +1,63 @@
// 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_corr_H_
#define _dsps_corr_H_
#include "dsp_err.h"
#include "dsps_conv_platform.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**@{*/
/**
* @brief Correlation with pattern
*
* The function correlate input sigla array with pattern array.
* The implementation use ANSI C and could be compiled and run on any platform
*
* @param[in] Signal: input array with signal values
* @param[in] siglen: length of the signal array
* @param[in] Pattern: input array with pattern values
* @param[in] patlen: length of the pattern array. The siglen must be bigger then patlen!
* @param dest: output array with result of correlation
*
* @return
* - ESP_OK on success
* - One of the error codes from DSP library (one of the input array are NULL, or if (siglen < patlen))
*/
esp_err_t dsps_corr_f32_ansi(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *dest);
esp_err_t dsps_corr_f32_ae32(const float *Signal, const int siglen, const float *Pattern, const int patlen, float *dest);
/**@}*/
#ifdef __cplusplus
}
#endif
#ifdef CONFIG_DSP_OPTIMIZED
#if (dsps_corr_f32_ae32_enabled == 1)
#define dsps_corr_f32 dsps_corr_f32_ae32
#else
#define dsps_corr_f32 dsps_corr_f32_ansi
#endif // dsps_corr_f32_ae32_enabled
#else
#define dsps_corr_f32 dsps_corr_f32_ansi
#endif
#endif // _dsps_corr_H_

View File

@@ -0,0 +1,118 @@
// 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 <string.h>
#include <math.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_dsp.h"
#include <malloc.h>
#include "dsp_tests.h"
static const char *TAG = "dspi_conv";
TEST_CASE("dspi_conv_f32_ansi functionality", "[dspi]")
{
int max_N = 8192;
float *data1 = (float *)memalign(16, max_N * sizeof(float));
float *data2 = (float *)memalign(16, max_N * sizeof(float));
float *data3 = (float *)memalign(16, max_N * sizeof(float));
image2d_t image1 = {data1, 1, 1, 8, 8, 8, 8}; // Image 8x8
image2d_t image2 = {data2, 1, 1, 4, 4, 4, 4}; // Image 4x4
image2d_t image3 = {data3, 1, 1, 10, 10, 0, 0}; // Image 8x8
for (int i = 0 ; i < max_N ; i++) {
data1[i] = 0;
data2[i] = 0;
data3[i] = 0;
}
for (int y = 0 ; y < image1.stride_y / image1.step_y ; y++) {
for (int x = 0 ; x < image1.stride_x / image1.step_x ; x++) {
data1[y * image1.stride_x * image1.step_y + x * image1.step_x] = 1;
}
}
for (int y = 0 ; y < image2.stride_y / image2.step_y ; y++) {
for (int x = 0 ; x < image2.stride_x / image2.step_x ; x++) {
data2[y * image2.stride_x * image2.step_y + x * image2.step_x] = 1;
}
}
dspi_conv_f32_ansi(&image1, &image2, &image3);
// x , y
TEST_ASSERT_EQUAL(data3[0 * image3.stride_x * image3.step_y + 0 * image3.step_x], 9);
TEST_ASSERT_EQUAL(data3[0 * image3.stride_x * image3.step_y + 6 * image3.step_x], 9);
TEST_ASSERT_EQUAL(data3[6 * image3.stride_x * image3.step_y + 6 * image3.step_x], 9);
TEST_ASSERT_EQUAL(data3[0 * image3.stride_x * image3.step_y + 6 * image3.step_x], 9);
TEST_ASSERT_EQUAL(data3[7 * image3.stride_x * image3.step_y + 0 * image3.step_x], 6);
TEST_ASSERT_EQUAL(data3[7 * image3.stride_x * image3.step_y + 6 * image3.step_x], 6);
TEST_ASSERT_EQUAL(data3[0 * image3.stride_x * image3.step_y + 7 * image3.step_x], 6);
TEST_ASSERT_EQUAL(data3[7 * image3.stride_x * image3.step_y + 7 * image3.step_x], 4);
TEST_ASSERT_EQUAL(data3[1 * image3.stride_x * image3.step_y + 1 * image3.step_x], 16);
TEST_ASSERT_EQUAL(data3[5 * image3.stride_x * image3.step_y + 1 * image3.step_x], 16);
TEST_ASSERT_EQUAL(data3[1 * image3.stride_x * image3.step_y + 5 * image3.step_x], 16);
TEST_ASSERT_EQUAL(data3[5 * image3.stride_x * image3.step_y + 5 * image3.step_x], 16);
TEST_ASSERT_EQUAL(data3[3 * image3.stride_x * image3.step_y + 3 * image3.step_x], 16);
free(data1);
free(data2);
free(data3);
}
TEST_CASE("dspi_conv_f32_ansi benchmark", "[dspi]")
{
int max_N = 8192;
float *data1 = (float *)memalign(16, max_N * sizeof(float));
float *data2 = (float *)memalign(16, max_N * sizeof(float));
float *data3 = (float *)memalign(16, max_N * sizeof(float));
image2d_t image1 = {data1, 1, 1, 8, 8, 8, 8}; // Image 8x8
image2d_t image2 = {data2, 1, 1, 4, 4, 4, 4}; // Image 4x4
image2d_t image3 = {data3, 1, 1, 10, 10, 0, 0}; // Image 8x8
for (int i = 0 ; i < max_N ; i++) {
data1[i] = 0;
data2[i] = 0;
data3[i] = 0;
}
for (int y = 0 ; y < image1.stride_y / image1.step_y ; y++) {
for (int x = 0 ; x < image1.stride_x / image1.step_x ; x++) {
data1[y * image1.stride_x * image1.step_y + x * image1.step_x] = 1;
}
}
for (int y = 0 ; y < image2.stride_y / image2.step_y ; y++) {
for (int x = 0 ; x < image2.stride_x / image2.step_x ; x++) {
data2[y * image2.stride_x * image2.step_y + x * image2.step_x] = 1;
}
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dspi_conv_f32_ansi(&image1, &image2, &image3);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dspi_conv_f32_ansi - %f cycles", cycles);
free(data1);
free(data2);
free(data3);
}

View File

@@ -0,0 +1,82 @@
// 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 <string.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsp_tests.h"
#include "dsps_ccorr.h"
#include "esp_attr.h"
static const char *TAG = "dsps_ccorr";
#define lenA 8
#define lenB 4
static float inputA[lenA];
static float inputB[lenB];
static float output[lenA + lenB - 1 + 2];
static float output_ref[lenA + lenB - 1 + 2];
TEST_CASE("dsps_ccorr_f32 functionality", "[dsps]")
{
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = i + 3;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = i + 10;
}
for (int i = 0 ; i < (lenA + lenB + 2 - 1); i++) {
output[i] = -1;
output_ref[i] = -1;
}
dsps_ccorr_f32(inputA, lenA, inputB, lenB, &output[0]);
dsps_ccorr_f32_ansi(inputA, lenA, inputB, lenB, &output_ref[0]);
for (int i = 0; i < (lenA + lenB - 1) + 2; i++) {
ESP_LOGI(TAG, "Data[%i] = %2.2f, expected = %2.2f", i, output[i], output_ref[i]);
}
for (size_t i = 0; i < (lenA + lenB - 1) + 2; i++) {
TEST_ASSERT_EQUAL(output_ref[i], output[i]);
}
}
TEST_CASE("dsps_ccorr_f32 benchmark", "[dsps]")
{
int max_N = 1024;
int ccorr_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N + ccorr_size - 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_ccorr_f32(x, max_N, y, ccorr_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_ccorr_f32 - %f cycles for signal %i and pattern %i", cycles, max_N, ccorr_size);
free(x);
free(y);
free(z);
}

View File

@@ -0,0 +1,116 @@
// Copyright 2018-2023 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 <math.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsps_ccorr.h"
#include "esp_attr.h"
#include "esp_dsp.h"
static const char *TAG = "dsps_ccorr";
#define lenA 20
#define lenB 20
static float inputA[lenA];
static float inputB[lenB];
static float output_fwd[lenA + lenB - 1 + 2];
static float output_back[lenA + lenB - 1 + 2];
TEST_CASE("dsps_ccorr_f32_ansi functionality", "[dsps]")
{
for (size_t la = 1; la < lenA; la++) {
for (size_t lb = 1; lb < lenB; lb++) {
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < (lenA + lenB - 1 + 2); i++) {
output_fwd[i] = -1;
output_back[i] = -1;
}
dsps_ccorr_f32_ansi(inputA, la, inputB, lb, &output_fwd[1]);
dsps_ccorr_f32_ansi(inputB, lb, inputA, la, &output_back[1]);
TEST_ASSERT_EQUAL(output_fwd[0], -1);
TEST_ASSERT_EQUAL(output_fwd[la + lb], -1);
TEST_ASSERT_EQUAL(output_back[0], -1);
TEST_ASSERT_EQUAL(output_back[la + lb], -1);
}
}
}
TEST_CASE("dsps_ccorr_f32_ansi draw", "[dsps]")
{
int max_N = 1024;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N * 2 + 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
int l1 = 8;
int l2 = 4;
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 0;
z[i] = 0;
}
x[0] = 20;
x[7] = 30;
y[0] = 10;
y[3] = 8;
dsps_ccorr_f32_ansi(x, l1, y, l2, &z[0]);
dsps_view(z, l1 + l2, l1 + l2, 10, -1, 400, '+');
for (int i = 0 ; i < (l1 + l2 - 1) ; i++) {
ESP_LOGI(TAG, "Z[%i] = %2.2f", i, z[i]);
}
free(x);
free(y);
free(z);
}
TEST_CASE("dsps_ccorr_f32_ansi benchmark", "[dsps]")
{
int max_N = 1024;
int conv_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N * 2 + 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_ccorr_f32_ansi(x, max_N, y, conv_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_conv_f32_ansi - %f cycles for signal %i and pattern %i", cycles, max_N, conv_size);
free(x);
free(y);
free(z);
}

View File

@@ -0,0 +1,143 @@
// Copyright 2018-2023 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 <math.h>
#include <malloc.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsp_tests.h"
#include "dsps_conv.h"
#include "esp_attr.h"
static const char *TAG = "dsps_conv";
#define lenA 30
#define lenB 30
TEST_CASE("dsps_conv_f32 test output", "[dsps]")
{
float *inputA = (float *)memalign(16, lenA * sizeof(float));
float *inputB = (float *)memalign(16, lenB * sizeof(float));
float *output_ref = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_fwd = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_back = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
int la = 3;
int lb = 2;
for (int i = 0; i < lenA; i++) {
inputA[i] = 10 + i;
}
for (int i = 0; i < lenB; i++) {
inputB[i] = 20 + i;
}
for (int i = 0; i < (lenA + lenB - 1 + 2); i++) {
output_ref[i] = -1;
output_fwd[i] = -1;
output_back[i] = -1;
}
dsps_conv_f32_ansi(inputA, la, inputB, lb, &output_ref[1]);
dsps_conv_f32(inputA, la, inputB, lb, &output_fwd[1]);
for (int i = 0; i < (la + lb + 1); i++) {
ESP_LOGD(TAG, "la=%i, lb=%i, i=%i, ref=%2.3f, fwd=%2.3f", la, lb, i, output_ref[i], output_fwd[i]);
}
float max_eps = 0.000001;
for (int i = 0; i < (la + lb + 1); i++) {
if (fabs(output_ref[i] - output_fwd[i]) > max_eps) {
ESP_LOGI(TAG, "la=%i, lb=%i, i=%i, ref=%2.3f, fwd=%2.3f", la, lb, i, output_ref[i], output_fwd[i]);
}
TEST_ASSERT_EQUAL(output_ref[i], output_fwd[i]);
}
free(inputA);
free(inputB);
free(output_ref);
free(output_fwd);
free(output_back);
}
TEST_CASE("dsps_conv_f32 functionality", "[dsps]")
{
float *inputA = (float *)memalign(16, lenA * sizeof(float));
float *inputB = (float *)memalign(16, lenB * sizeof(float));
float *output_ref = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_fwd = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_back = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
for (int la = 2; la < lenA; la++) {
for (int lb = 2; lb < lenB; lb++) {
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < (lenA + lenB - 1 + 2); i++) {
output_ref[i] = -1;
output_fwd[i] = -1;
output_back[i] = -1;
}
dsps_conv_f32_ansi(inputA, la, inputB, lb, &output_ref[1]);
dsps_conv_f32(inputA, la, inputB, lb, &output_fwd[1]);
dsps_conv_f32(inputB, lb, inputA, la, &output_back[1]);
float max_eps = 0.000001;
for (int i = 0; i < (la + lb + 1); i++) {
if ((fabs(output_ref[i] - output_fwd[i]) > max_eps) || (fabs(output_ref[i] - output_back[i]) > max_eps) || (fabs(output_back[i] - output_fwd[i]) > max_eps)) {
ESP_LOGI(TAG, "la=%i, lb=%i, i=%i, ref=%2.3f, fwd=%2.3f, back=%2.3f", la, lb, i, output_ref[i], output_fwd[i], output_back[i]);
}
TEST_ASSERT_EQUAL(output_ref[i], output_fwd[i]);
TEST_ASSERT_EQUAL(output_ref[i], output_back[i]);
TEST_ASSERT_EQUAL(output_back[i], output_fwd[i]);
}
}
}
free(inputA);
free(inputB);
free(output_ref);
free(output_fwd);
free(output_back);
}
TEST_CASE("dsps_conv_f32 benchmark", "[dsps]")
{
int max_N = 1024;
int conv_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N * 2 + 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_conv_f32(x, max_N, y, conv_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_conv_f32 - %f cycles for signal %i and pattern %i", cycles, max_N, conv_size);
free(x);
free(y);
free(z);
}

View File

@@ -0,0 +1,152 @@
// Copyright 2018-2023 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 <math.h>
#include <malloc.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsp_tests.h"
#include "dsps_conv.h"
#include "esp_attr.h"
#include "esp_dsp.h"
static const char *TAG = "dsps_conv";
#define lenA 20
#define lenB 20
esp_err_t dsps_conv_f32_ref(const float *Signal, const int siglen, const float *Kernel, const int kernlen, float *convout)
{
if (NULL == Signal) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == Kernel) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
if (NULL == convout) {
return ESP_ERR_DSP_PARAM_OUTOFRANGE;
}
for (int n = 0; n < siglen + kernlen - 1; n++) {
size_t kmin, kmax, k;
convout[n] = 0;
kmin = (n >= kernlen - 1) ? n - (kernlen - 1) : 0;
kmax = (n < siglen - 1) ? n : siglen - 1;
for (k = kmin; k <= kmax; k++) {
convout[n] += Signal[k] * Kernel[n - k];
}
}
return ESP_OK;
}
TEST_CASE("dsps_conv_f32_ansi functionality", "[dsps]")
{
float *inputA = (float *)memalign(16, lenA * sizeof(float));
float *inputB = (float *)memalign(16, lenB * sizeof(float));
float *output_ref = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_fwd = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
float *output_back = (float *)memalign(16, (lenA + lenB - 1 + 2) * sizeof(float));
for (int la = 1; la < lenA; la++) {
for (int lb = 1; lb < lenB; lb++) {
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = (float)rand() / (float)INT32_MAX;
}
for (int i = 0 ; i < (lenA + lenB - 1 + 2); i++) {
output_ref[i] = -1;
output_fwd[i] = -1;
output_back[i] = -1;
}
dsps_conv_f32_ref(inputA, la, inputB, lb, &output_ref[1]);
dsps_conv_f32_ansi(inputA, la, inputB, lb, &output_fwd[1]);
dsps_conv_f32_ansi(inputB, lb, inputA, la, &output_back[1]);
float max_eps = 0.000001;
for (int i = 0; i < (la + lb + 1); i++) {
if ((fabs(output_ref[i] - output_fwd[i]) > max_eps) || (fabs(output_ref[i] - output_back[i]) > max_eps) || (fabs(output_back[i] - output_fwd[i]) > max_eps)) {
ESP_LOGI(TAG, "la=%i, lb=%i, i=%i, ref=%2.3f, fwd=%2.3f, back=%2.3f", la, lb, i, output_ref[i], output_fwd[i], output_back[i]);
}
TEST_ASSERT_EQUAL(output_ref[i], output_fwd[i]);
TEST_ASSERT_EQUAL(output_ref[i], output_back[i]);
TEST_ASSERT_EQUAL(output_back[i], output_fwd[i]);
}
}
}
free(inputA);
free(inputB);
free(output_ref);
free(output_fwd);
free(output_back);
}
TEST_CASE("dsps_conv_f32_ansi draw", "[dsps]")
{
int max_N = 1024;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N * 2 + 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 10;
y[i] = 20;
z[i] = 0;
}
dsps_conv_f32_ansi(x, 32, y, 16, &z[0]);
dsps_view(z, 32 + 16, 32 + 16, 10, -1, 4000, '+');
free(x);
free(y);
free(z);
}
TEST_CASE("dsps_conv_f32_ansi benchmark", "[dsps]")
{
int max_N = 1024;
int conv_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc((max_N * 2 + 1) * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_conv_f32_ansi(x, max_N, y, conv_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_conv_f32_ansi - %f cycles for signal %i and pattern %i", cycles, max_N, conv_size);
free(x);
free(y);
free(z);
}

View File

@@ -0,0 +1,83 @@
// 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 <string.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsp_tests.h"
#include "dsps_corr.h"
#include "esp_attr.h"
static const char *TAG = "dsps_corr";
#define lenA 15
#define lenB 10
static float inputA[lenA];
static float inputB[lenB];
static float output[lenA + lenB - 1 + 2];
static float output_ref[lenA + lenB - 1 + 2];
TEST_CASE("dsps_corr_f32_aexx functionality", "[dsps]")
{
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = i;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = 10 + i;
}
for (int i = 0 ; i < (lenA - lenB + 2); i++) {
output[i] = -1;
output_ref[i] = -1;
}
inputB[0] = 1;
dsps_corr_f32(inputA, lenA, inputB, lenB, &output[1]);
dsps_corr_f32_ansi(inputA, lenA, inputB, lenB, &output_ref[1]);
for (int i = 0; i < (lenA - lenB) + 2; i++) {
ESP_LOGD(TAG, "Data[%i] = %2.2f, expected = %2.2f", i, output[i], output_ref[i]);
}
for (size_t i = 0; i < (lenA - lenB) + 2; i++) {
TEST_ASSERT_EQUAL(output_ref[i], output[i]);
}
}
TEST_CASE("dsps_corr_f32_aexx benchmark", "[dsps]")
{
int max_N = 1024;
int corr_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_corr_f32(x, max_N, y, corr_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_corr_f32_ae32 - %f cycles for signal %i and pattern %i", cycles, max_N, corr_size);
free(x);
free(y);
free(z);
}

View File

@@ -0,0 +1,83 @@
// 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 <string.h>
#include "unity.h"
#include "dsp_platform.h"
#include "esp_log.h"
#include "dsp_tests.h"
#include "dsps_corr.h"
#include "esp_attr.h"
static const char *TAG = "dsps_corr";
#define lenA 15
#define lenB 10
static float inputA[lenA];
static float inputB[lenB];
static float output[lenA + lenB + 2];
TEST_CASE("dsps_corr_f32_ansi functionality", "[dsps]")
{
for (int i = 0 ; i < lenA ; i++) {
inputA[i] = i;
}
for (int i = 0 ; i < lenB ; i++) {
inputB[i] = 0;
}
for (int i = 0 ; i <= (lenA - lenB + 2); i++) {
output[i] = -1;
}
inputB[0] = 1;
dsps_corr_f32_ansi(inputA, lenA, inputB, lenB, &output[1]);
for (int i = 0; i < lenA + lenB; i++) {
ESP_LOGD(TAG, "output[%i] = %2.2f", i, output[i]);
}
TEST_ASSERT_EQUAL(output[0], -1);
TEST_ASSERT_EQUAL(output[lenA - lenB + 2], -1);
for (size_t i = 0; i <= (lenA - lenB); i++) {
TEST_ASSERT_EQUAL(output[i + 1], i);
}
}
TEST_CASE("dsps_corr_f32_ansi benchmark", "[dsps]")
{
int max_N = 1024;
int corr_size = 64;
float *x = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(x);
float *y = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(y);
float *z = (float *)malloc(max_N * sizeof(float));
TEST_ASSERT_NOT_NULL(z);
for (int i = 0 ; i < max_N ; i++) {
x[i] = 0;
y[i] = 1000;
}
unsigned int start_b = dsp_get_cpu_cycle_count();
dsps_corr_f32_ansi(x, max_N, y, corr_size, &z[0]);
unsigned int end_b = dsp_get_cpu_cycle_count();
float cycles = end_b - start_b;
ESP_LOGI(TAG, "dsps_corr_f32_ansi - %f cycles for signal %i and pattern %i", cycles, max_N, corr_size);
free(x);
free(y);
free(z);
}