Advertisement
Attilator

128 channel spectrum analyzer

Jan 16th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Arduino Spectrum Analyzer
  3.  *
  4.  * learnelectronics
  5.  * 27 April 2017
  6.  * black magic stolen from CBM80Amiga
  7.  *
  8.  * www.youtube.com/c/learnelectronics
  9.  * arduino1069@gmail.com
  10.  *
  11.  * Fix_FFT library available @ https://github.com/kosme/arduinoFFT
  12.  */
  13.  
  14.  
  15. #include "fix_fft.h"                                  //library to perfom the Fast Fourier Transform
  16.  
  17. #include <SPI.h>                                      //SPI library
  18. //#include <Wire.h>                                     //I2C library for OLED
  19. #include <Adafruit_GFX.h>                             //graphics library for OLED
  20. #include <Adafruit_SH1106.h>                          //OLED driver
  21.  
  22. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  23. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  24.  
  25. // Comment out above, uncomment this block to use hardware SPI, MOSI-11, CLK-13
  26. #define OLED_DC     6
  27. #define OLED_CS     7
  28. #define OLED_RESET  8
  29. Adafruit_SH1106 display(OLED_DC, OLED_RESET, OLED_CS);
  30.  
  31.  
  32. //#define OLED_RESET 4                                  //OLED reset, not hooked up but required by library
  33.  
  34. char im[128], data[128];                              //variables for the FFT
  35. char x = 0, ylim = 60;                                //variables for drawing the graphics
  36. int i = 0, val;                                       //counters
  37.  
  38. void setup()
  39. {
  40.   Serial.begin(9600);                                 //serial comms for debuging
  41.   display.begin(SH1106_SWITCHCAPVCC);
  42.   display.setTextSize(1);                             //set OLED text size to 1 (1-6)
  43.   display.setTextColor(WHITE);                        //set text color to white
  44.   display.clearDisplay();                             //clear display
  45.   analogReference(DEFAULT);                           // Use default (5v) aref voltage.
  46. };
  47.  
  48. void loop()
  49. {
  50.   int min=1024, max=0;                                //set minumum & maximum ADC values
  51.   for (i = 0; i < 128; i++) {                         //take 128 samples
  52.     val = analogRead(A0);                             //get audio from Analog 0
  53.     data[i] = val / 4 - 128;                          //each element of array is val/4-128
  54.     im[i] = 0;                                        //
  55.     if(val>max) max=val;                              //capture maximum level
  56.     if(val<min) min=val;                              //capture minimum level
  57.   };
  58.    
  59.   fix_fft(data, im, 7, 0);                            //perform the FFT on data
  60.  
  61.   display.clearDisplay();                             //clear display
  62.   for (i = 1; i < 64; i++) {                          // In the current design, 60Hz and noise
  63.     int dat = sqrt(data[i] * data[i] + im[i] * im[i]);//filter out noise and hum
  64.     display.drawLine(i*2 + x, ylim, i*2 + x, ylim - dat, WHITE); // draw bar graphics for freqs above 500Hz to buffer
  65.   };                                                
  66.   display.setCursor(0,0);                             //set cursor to top of screen
  67.   display.print("->Spectrum Analyzer<-");             //print title to buffer
  68.   display.display();                                  //show the buffer
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement