Skip to content

Commit

Permalink
variable sample rate; transmit full FFT spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanu Frechen committed Oct 8, 2023
1 parent f9471de commit 6755f84
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
49 changes: 36 additions & 13 deletions FFT_visualisation/FFT_visualisation.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ int xStart = 70;
int xPos = xStart; // horizontal position of the graph
float speed = 0;
float old_speed = 0;
int[] nums = new int[513];
int num_fft_bins = 200;
float[] nums = new float[num_fft_bins+2];
float spec_speed = 0;
float speed_conversion = 0.9787819602; //44100/1024/44; //1.578125;
int max_frequency = 51;
int sample_rate = 11000;
float fft_bin_width = sample_rate/1024;
float speed_conversion = fft_bin_width/44.0; //44100/1024/44; //1.578125;
int max_frequency = 100;

String inString;
float peak;
float oldpeak = 0;


void setup () {
// set the window size:
Expand Down Expand Up @@ -73,13 +80,15 @@ void draw () {
//fill(xPos, 100, nums[i]);
spec_speed = i+2.5;
spec_speed = map(spec_speed, 1, max_frequency, 0, height);
stroke(255-float(nums[i])*10);
stroke(255-nums[i]*10);
rect(xPos, height-spec_speed, 1, height/max_frequency);
}

// draw the line:
stroke(#ff0000);
line(xPos-1, height-old_speed, xPos, height - speed);
if(peak>0.01){
//line(xPos-1, height-old_speed, xPos, height - speed);
}

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
Expand All @@ -93,20 +102,28 @@ void draw () {
//draw speed as text
fill(255);
stroke(255);
rect(width-120, 0, 120, 80);
rect(width-220, 0, 220, 90);
fill(#ff0000);
textSize(104);
textAlign(RIGHT);
text(round(nums[126]*speed_conversion), width, 80);
if(peak > 0.01){
text(round(speed), width, 80);
}
if(peak >= 1.0){
fill(#ff0000);
}else{
fill(#51A351);
}
rect(width/2-50, 0, 50, 30);
}

void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
max_frequency++;
max_frequency = min(num_fft_bins, max_frequency+1);
draw_axis();
} else if (keyCode == DOWN) {
max_frequency--;
max_frequency = max(40, max_frequency-1);
draw_axis();
}
} else if (key == 's') {
Expand Down Expand Up @@ -140,10 +157,16 @@ void serialEvent (Serial myPort) {
if (inString != null) {
old_speed = speed;

nums = int(trim(split(inString, ',')));
nums = float(trim(split(inString, ',')));
// trim off any whitespace:
//println(nums[126]);
speed = map(nums[155], 0, max_frequency, 0, height);
//speed = map(4, 0, max_frequency, 0, height);
//println(nums[155]);
if(nums[num_fft_bins+1]>0.0 & nums[num_fft_bins+1]/44 < 150){
speed = map(nums[num_fft_bins+1], 1, max_frequency, 0, height) + fft_bin_width/44;
//speed = map(4, 0, max_frequency, 0, height);
}
peak = nums[num_fft_bins+2];
oldpeak = max(peak, oldpeak);
println(round(oldpeak*100)/100.0);
oldpeak = oldpeak*0.999;
}
}
24 changes: 22 additions & 2 deletions Teensy_fft.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <utility/imxrt_hw.h>

void setI2SFreq(int freq) {
// PLL between 27*24 = 648MHz und 54*24=1296MHz
int n1 = 4; //SAI prescaler 4 => (n1*n2) = multiple of 4
int n2 = 1 + (24000000 * 27) / (freq * 256 * n1);
double C = ((double)freq * 256 * n1 * n2) / 24000000;
int c0 = C;
int c2 = 10000;
int c1 = C * c2 - (c0 * c2);
set_audioClock(c0, c1, c2, true);
CCM_CS1CDR = (CCM_CS1CDR & ~(CCM_CS1CDR_SAI1_CLK_PRED_MASK | CCM_CS1CDR_SAI1_CLK_PODF_MASK))
| CCM_CS1CDR_SAI1_CLK_PRED(n1-1) // &0x07
| CCM_CS1CDR_SAI1_CLK_PODF(n2-1); // &0x3f
//Serial.printf("SetI2SFreq(%d)\n",freq);
}

AudioAnalyzeFFT1024 fft1024_1; //xy=862.3333129882812,439.33331298828125
AudioAmplifier amp1; //xy=425,443
Expand Down Expand Up @@ -74,14 +90,16 @@ short filt_coeff[TAP_NUM] = {


void setup() {
setI2SFreq(11000);

// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(60);

// Enable the audio shield, select input, and enable output
sgtl5000_1.enable();
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.micGain(50); //16 is good
sgtl5000_1.micGain(8); //16 is good
//sgtl5000_1.lineInLevel(0);
sgtl5000_1.volume(.5);

Expand Down Expand Up @@ -116,8 +134,10 @@ void loop() {
if(fft1024_1.available())
{



// max speed fft bin: 154*(44100/1024)/44 = 150,732 km/h
for(i = 0; i <= 154; i++)
for(i = 0; i <= 511; i++)
{
Serial.print((int16_t)(fft1024_1.read(i) * 1000));

Expand Down

0 comments on commit 6755f84

Please sign in to comment.