-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d2bd9a7
Showing
13 changed files
with
1,414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
cmake_minimum_required(VERSION 3.12) | ||
|
||
include(pico_sdk_import.cmake) | ||
|
||
project(adc_fft_project) | ||
|
||
pico_sdk_init() | ||
|
||
add_executable(adc_fft adc_fft.c) | ||
add_library(kiss_fftr kiss_fftr.c) | ||
add_library(kiss_fft kiss_fft.c) | ||
|
||
target_link_libraries(kiss_fftr kiss_fft) | ||
|
||
pico_enable_stdio_usb(adc_fft 1) | ||
pico_enable_stdio_uart(adc_fft 1) | ||
|
||
pico_add_extra_outputs(adc_fft) | ||
|
||
target_link_libraries(adc_fft | ||
pico_stdlib | ||
hardware_adc | ||
hardware_dma | ||
kiss_fftr | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
Copyright (c) 2003-2010, Mark Borgerding | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
/* kiss_fft.h | ||
defines kiss_fft_scalar as either short or a float type | ||
and defines | ||
typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ | ||
#include "kiss_fft.h" | ||
#include <limits.h> | ||
|
||
#define MAXFACTORS 32 | ||
/* e.g. an fft of length 128 has 4 factors | ||
as far as kissfft is concerned | ||
4*4*4*2 | ||
*/ | ||
|
||
struct kiss_fft_state{ | ||
int nfft; | ||
int inverse; | ||
int factors[2*MAXFACTORS]; | ||
kiss_fft_cpx twiddles[1]; | ||
}; | ||
|
||
/* | ||
Explanation of macros dealing with complex math: | ||
C_MUL(m,a,b) : m = a*b | ||
C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise | ||
C_SUB( res, a,b) : res = a - b | ||
C_SUBFROM( res , a) : res -= a | ||
C_ADDTO( res , a) : res += a | ||
* */ | ||
#ifdef FIXED_POINT | ||
#if (FIXED_POINT==32) | ||
# define FRACBITS 31 | ||
# define SAMPPROD int64_t | ||
#define SAMP_MAX 2147483647 | ||
#else | ||
# define FRACBITS 15 | ||
# define SAMPPROD int32_t | ||
#define SAMP_MAX 32767 | ||
#endif | ||
|
||
#define SAMP_MIN -SAMP_MAX | ||
|
||
#if defined(CHECK_OVERFLOW) | ||
# define CHECK_OVERFLOW_OP(a,op,b) \ | ||
if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ | ||
fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } | ||
#endif | ||
|
||
|
||
# define smul(a,b) ( (SAMPPROD)(a)*(b) ) | ||
# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) | ||
|
||
# define S_MUL(a,b) sround( smul(a,b) ) | ||
|
||
# define C_MUL(m,a,b) \ | ||
do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ | ||
(m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) | ||
|
||
# define DIVSCALAR(x,k) \ | ||
(x) = sround( smul( x, SAMP_MAX/k ) ) | ||
|
||
# define C_FIXDIV(c,div) \ | ||
do { DIVSCALAR( (c).r , div); \ | ||
DIVSCALAR( (c).i , div); }while (0) | ||
|
||
# define C_MULBYSCALAR( c, s ) \ | ||
do{ (c).r = sround( smul( (c).r , s ) ) ;\ | ||
(c).i = sround( smul( (c).i , s ) ) ; }while(0) | ||
|
||
#else /* not FIXED_POINT*/ | ||
|
||
# define S_MUL(a,b) ( (a)*(b) ) | ||
#define C_MUL(m,a,b) \ | ||
do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ | ||
(m).i = (a).r*(b).i + (a).i*(b).r; }while(0) | ||
# define C_FIXDIV(c,div) /* NOOP */ | ||
# define C_MULBYSCALAR( c, s ) \ | ||
do{ (c).r *= (s);\ | ||
(c).i *= (s); }while(0) | ||
#endif | ||
|
||
#ifndef CHECK_OVERFLOW_OP | ||
# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ | ||
#endif | ||
|
||
#define C_ADD( res, a,b)\ | ||
do { \ | ||
CHECK_OVERFLOW_OP((a).r,+,(b).r)\ | ||
CHECK_OVERFLOW_OP((a).i,+,(b).i)\ | ||
(res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ | ||
}while(0) | ||
#define C_SUB( res, a,b)\ | ||
do { \ | ||
CHECK_OVERFLOW_OP((a).r,-,(b).r)\ | ||
CHECK_OVERFLOW_OP((a).i,-,(b).i)\ | ||
(res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ | ||
}while(0) | ||
#define C_ADDTO( res , a)\ | ||
do { \ | ||
CHECK_OVERFLOW_OP((res).r,+,(a).r)\ | ||
CHECK_OVERFLOW_OP((res).i,+,(a).i)\ | ||
(res).r += (a).r; (res).i += (a).i;\ | ||
}while(0) | ||
|
||
#define C_SUBFROM( res , a)\ | ||
do {\ | ||
CHECK_OVERFLOW_OP((res).r,-,(a).r)\ | ||
CHECK_OVERFLOW_OP((res).i,-,(a).i)\ | ||
(res).r -= (a).r; (res).i -= (a).i; \ | ||
}while(0) | ||
|
||
|
||
#ifdef FIXED_POINT | ||
# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) | ||
# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) | ||
# define HALF_OF(x) ((x)>>1) | ||
#elif defined(USE_SIMD) | ||
# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) | ||
# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) | ||
# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) | ||
#else | ||
# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) | ||
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) | ||
# define HALF_OF(x) ((x)*.5) | ||
#endif | ||
|
||
#define kf_cexp(x,phase) \ | ||
do{ \ | ||
(x)->r = KISS_FFT_COS(phase);\ | ||
(x)->i = KISS_FFT_SIN(phase);\ | ||
}while(0) | ||
|
||
|
||
/* a debugging function */ | ||
#define pcpx(c)\ | ||
fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) | ||
|
||
|
||
#ifdef KISS_FFT_USE_ALLOCA | ||
// define this to allow use of alloca instead of malloc for temporary buffers | ||
// Temporary buffers are used in two case: | ||
// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 | ||
// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. | ||
#include <alloca.h> | ||
#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) | ||
#define KISS_FFT_TMP_FREE(ptr) | ||
#else | ||
#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) | ||
#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Sample from the ADC continuously at a particular sample rate | ||
// and then compute an FFT over the data | ||
// | ||
// much of this code is from pico-examples/adc/dma_capture/dma_capture.c | ||
// the rest is written by Alex Wulff (www.AlexWulff.com) | ||
|
||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
#include "pico/stdlib.h" | ||
#include "hardware/adc.h" | ||
#include "hardware/dma.h" | ||
#include "kiss_fftr.h" | ||
|
||
// set this to determine sample rate | ||
// 0 = 500,000 Hz | ||
// 960 = 50,000 Hz | ||
// 9600 = 5,000 Hz | ||
#define CLOCK_DIV 960 | ||
#define FSAMP 50000 | ||
|
||
// Channel 0 is GPIO26 | ||
#define CAPTURE_CHANNEL 0 | ||
#define LED_PIN 25 | ||
|
||
// BE CAREFUL: anything over about 9000 here will cause things | ||
// to silently break. The code will compile and upload, but due | ||
// to memory issues nothing will work properly | ||
#define NSAMP 1000 | ||
|
||
// globals | ||
dma_channel_config cfg; | ||
uint dma_chan; | ||
float freqs[NSAMP]; | ||
|
||
void setup(); | ||
void sample(uint8_t *capture_buf); | ||
|
||
int main() { | ||
uint8_t cap_buf[NSAMP]; | ||
kiss_fft_scalar fft_in[NSAMP]; // kiss_fft_scalar is a float | ||
kiss_fft_cpx fft_out[NSAMP]; | ||
kiss_fftr_cfg cfg = kiss_fftr_alloc(NSAMP,false,0,0); | ||
|
||
// setup ports and outputs | ||
setup(); | ||
|
||
while (1) { | ||
// get NSAMP samples at FSAMP | ||
sample(cap_buf); | ||
// fill fourier transform input while subtracting DC component | ||
uint64_t sum = 0; | ||
for (int i=0;i<NSAMP;i++) {sum+=cap_buf[i];} | ||
float avg = (float)sum/NSAMP; | ||
for (int i=0;i<NSAMP;i++) {fft_in[i]=(float)cap_buf[i]-avg;} | ||
|
||
// compute fast fourier transform | ||
kiss_fftr(cfg , fft_in, fft_out); | ||
|
||
// compute power and calculate max freq component | ||
float max_power = 0; | ||
int max_idx = 0; | ||
// any frequency bin over NSAMP/2 is aliased (nyquist sampling theorum) | ||
for (int i = 0; i < NSAMP/2; i++) { | ||
float power = fft_out[i].r*fft_out[i].r+fft_out[i].i*fft_out[i].i; | ||
if (power>max_power) { | ||
max_power=power; | ||
max_idx = i; | ||
} | ||
} | ||
|
||
float max_freq = freqs[max_idx]; | ||
printf("Greatest Frequency Component: %0.1f Hz\n",max_freq); | ||
} | ||
|
||
// should never get here | ||
kiss_fft_free(cfg); | ||
} | ||
|
||
void sample(uint8_t *capture_buf) { | ||
adc_fifo_drain(); | ||
adc_run(false); | ||
|
||
dma_channel_configure(dma_chan, &cfg, | ||
capture_buf, // dst | ||
&adc_hw->fifo, // src | ||
NSAMP, // transfer count | ||
true // start immediately | ||
); | ||
|
||
gpio_put(LED_PIN, 1); | ||
adc_run(true); | ||
dma_channel_wait_for_finish_blocking(dma_chan); | ||
gpio_put(LED_PIN, 0); | ||
} | ||
|
||
void setup() { | ||
stdio_init_all(); | ||
|
||
gpio_init(LED_PIN); | ||
gpio_set_dir(LED_PIN, GPIO_OUT); | ||
|
||
adc_gpio_init(26 + CAPTURE_CHANNEL); | ||
|
||
adc_init(); | ||
adc_select_input(CAPTURE_CHANNEL); | ||
adc_fifo_setup( | ||
true, // Write each completed conversion to the sample FIFO | ||
true, // Enable DMA data request (DREQ) | ||
1, // DREQ (and IRQ) asserted when at least 1 sample present | ||
false, // We won't see the ERR bit because of 8 bit reads; disable. | ||
true // Shift each sample to 8 bits when pushing to FIFO | ||
); | ||
|
||
// set sample rate | ||
adc_set_clkdiv(CLOCK_DIV); | ||
|
||
sleep_ms(1000); | ||
// Set up the DMA to start transferring data as soon as it appears in FIFO | ||
uint dma_chan = dma_claim_unused_channel(true); | ||
cfg = dma_channel_get_default_config(dma_chan); | ||
|
||
// Reading from constant address, writing to incrementing byte addresses | ||
channel_config_set_transfer_data_size(&cfg, DMA_SIZE_8); | ||
channel_config_set_read_increment(&cfg, false); | ||
channel_config_set_write_increment(&cfg, true); | ||
|
||
// Pace transfers based on availability of ADC samples | ||
channel_config_set_dreq(&cfg, DREQ_ADC); | ||
|
||
// calculate frequencies of each bin | ||
float f_max = FSAMP; | ||
float f_res = f_max / NSAMP; | ||
for (int i = 0; i < NSAMP; i++) {freqs[i] = f_res*i;} | ||
} |
Oops, something went wrong.