FastLED 3.9.15
Loading...
Searching...
No Matches
cq_kernel.h
Go to the documentation of this file.
1// Copyright 2020 Kenny Peng
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef CQ_KERNEL_H
16#define CQ_KERNEL_H
17
18#include <math.h>
19#include "_kiss_fft_guts.h"
20#include "kiss_fftr.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
30
31/*
32 * cq_kernel_cfg - holds parameters used by cq_kernel
33 *
34 * Initialize with desired parameters and pass into cq_kernel functions
35 * */
37 int samples; // kiss_fftr requires this must be even
38 int bands;
39 float fmin;
40 float fmax;
41 float fs;
43 kiss_fft_scalar min_val; // sparse matrix threshold, see CQT paper
44};
45
46/*
47 * sparse_arr_elem - holds one position and complex value pair in sparse array
48 * sparse_arr - holds length and pointer of sparse array
49 * */
57};
58
59typedef struct sparse_arr *cq_kernels_t;
60
61// private functions
62void _generate_center_freqs(float freq[], int bands, float fmin, float fmax);
63void _generate_hamming(kiss_fft_scalar window[], int N);
64void _generate_gaussian(kiss_fft_scalar window[], int N);
65void _generate_kernel(kiss_fft_cpx K[], kiss_fftr_cfg cfg, enum window_type window_type, float f, float fmin, float fs, int N);
67
68// public functions
69/*
70 * generate_kernels - generates constant q kernels from cfg
71 *
72 * typical usage: cq_kernels_t kernels = generate_kernels(cfg);
73 *
74 * Beware massive memory usage, uses more than 3*cfg.samples*sizeof(kiss_fft_scalar) bytes at a time
75 * to calculate all of the kernels. However, it will return sparse arrays that use much less memory.
76 * */
77struct sparse_arr* generate_kernels(struct cq_kernel_cfg cfg);
78
79/*
80 * reallocate_kernels - (optional) reallocates the kernels
81 *
82 * typical usage: kernels = reallocate_kernels(kernels, cfg);
83 *
84 * This can be used to eliminate the hole in heap left by generate_kernels
85 * */
86struct sparse_arr* reallocate_kernels(struct sparse_arr* kernels, struct cq_kernel_cfg cfg);
87
88/*
89 * apply_kernels - calculates constant q from output of kiss_fft
90 *
91 * typical usage:
92 * kiss_fft_cpx fft[SAMPLES] = {0};
93 * kiss_fftr(fft_cfg, in, fft);
94 * kiss_fft_cpx cq[BANDS] = {0};
95 * apply_kernels(fft, cq, kernels, cfg);
96 * */
97void apply_kernels(kiss_fft_cpx fft[], kiss_fft_cpx cq[], struct sparse_arr kernels[], struct cq_kernel_cfg cfg);
98
99/*
100 * free_kernels - frees memory used by the sparse arrays of kernels
101 *
102 * typical_usage: free_kernels(kernels, cfg);
103 * */
104void free_kernels(struct sparse_arr *kernels, struct cq_kernel_cfg cfg);
105
106#ifdef __cplusplus
107}
108#endif
109
110#endif
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
AudioAnalyzeFFT1024 fft
void _generate_gaussian(kiss_fft_scalar window[], int N)
void _generate_hamming(kiss_fft_scalar window[], int N)
Definition cq_kernel.c:30
void _generate_kernel(kiss_fft_cpx K[], kiss_fftr_cfg cfg, enum window_type window_type, float f, float fmin, float fs, int N)
Definition cq_kernel.c:52
struct sparse_arr * reallocate_kernels(struct sparse_arr *kernels, struct cq_kernel_cfg cfg)
Definition cq_kernel.c:128
window_type
Definition cq_kernel.h:26
@ HAMMING
Definition cq_kernel.h:27
@ GAUSSIAN
Definition cq_kernel.h:28
kiss_fft_scalar _mag(kiss_fft_cpx x)
Definition cq_kernel.c:84
void free_kernels(struct sparse_arr *kernels, struct cq_kernel_cfg cfg)
Definition cq_kernel.c:150
void apply_kernels(kiss_fft_cpx fft[], kiss_fft_cpx cq[], struct sparse_arr kernels[], struct cq_kernel_cfg cfg)
Definition cq_kernel.c:140
struct sparse_arr * cq_kernels_t
Definition cq_kernel.h:59
struct sparse_arr * generate_kernels(struct cq_kernel_cfg cfg)
Definition cq_kernel.c:88
void _generate_center_freqs(float freq[], int bands, float fmin, float fmax)
Definition cq_kernel.c:25
struct sparse_arr_elem * elems
Definition cq_kernel.h:56
kiss_fft_cpx val
Definition cq_kernel.h:52
int n_elems
Definition cq_kernel.h:55
enum window_type window_type
Definition cq_kernel.h:42
kiss_fft_scalar min_val
Definition cq_kernel.h:43
#define kiss_fft_scalar
Definition kiss_fft.h:63
struct kiss_fftr_state * kiss_fftr_cfg
Definition kiss_fftr.h:26