2026年3月1日日曜日

Pico FFT - Audio Spectrum Analyzer

Raspberry Pi PicoによるFFT Audio Spectrum Analyzerにサイン波5KHzを入力してOLEDにスペクトラム表示しました。 

※OLEDスペクトラム表示はgithub.com/JR3XNW【Filter_sweep_test_SpectrumDisplay_Bar01】プログラムを使用しました。  










参考サイト
●github.com/JR3XNW
●ラズピコを使ってステレオのスペクトラムアナライザを作る



信号発生器SG (5KHz)出力波形


















スペクトラム表示
結線
OLED     Pico
SDA   -   GP16
SCL   -   GP17
VCC   -   3V3
GND   -   GND
IN   -    GP26 ← SG
●入力信号は信号発生器SG (5KHz)
同ブログ









プログラム Arduino IDE【ボード:Raspberry Pi Pico】
//JR3XNW Filter_sweep_test_SpectrumDisplay_Bar01.ino

#include <Arduino.h>
#include <U8g2lib.h>
#include <arduinoFFT.h> // v2.0.2
#include <Wire.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); //ディスプレイ設定
ArduinoFFT<double> FFT;  // v2.0.2 Explicit data types using templates
const int samples = 128; // Number of FFT samples.
double vReal[samples];
double vImag[samples] = {0}; // Imaginary part is initialized to 0.

// frequency sweep function.
void frequencySweep(double *vReal, double *vImag) {  
  for (int i = 0; i < samples; i++) {      
    vReal[i] = (analogRead(26) - 2048)*3.3 / 4096.0; //GP26信号入力(電圧に換算)    
    vImag[i] = 0;
    delayMicroseconds(21); // サンプリング周期調整  
  }
}
// FFT and OLED display functions (line graph display).
void displayFFT(double *vReal, double *vImag) {
  FFT.windowing(vReal, samples, FFTWindow::Hamming, FFTDirection::Forward); //vReal 窓関数
  FFT.windowing(vImag, samples, FFTWindow::Hamming, FFTDirection::Forward); //vImag 窓関数
  FFT.compute(vReal, vImag, samples, FFTDirection::Forward);  //FFT処理(複素数で計算)
  FFT.complexToMagnitude(vReal, vImag, samples);  // 複素数を実数に変換
  // Clear graph drawing area (overwrite with background color).
  // Here, the graph area is overwritten with a black rectangle.
  u8g2.setDrawColor(0); // Set background color (usually black).
  u8g2.drawBox(0, 0, 128, 52); // Clear graph area.■(始点のX座標, 始点のY座標, 幅, 高さ)
  u8g2.setDrawColor(1); // Re-set the drawing color (white).
  for (int i = 1; i < (samples / 2); i++) {
    int x = map(i, 1, samples / 3.3, 0, 128); //周波数軸(現数値,現下限,現上限,変換下限,変換上限)
    int y = map(vReal[i], 0, 4, 53, 0); //信号強度(現数値,現下限,現上限,変換下限,変換上限)
    u8g2.drawLine(x, 53, x, y); //スペクトラム表示(始点X座標, 始点Y座標, 終点X座標, 終点Y座標)
  }
  // Send buffer to update screen.
  u8g2.sendBuffer();
}

void setup() {
  Wire.setSDA(16);  //SDA
  Wire.setSCL(17);  //SCL
  Wire.begin();  
  u8g2.begin();
  // Redraw the static content (lines and texts) each time to prevent flickering
  u8g2.drawFrame(0, 0, 128, 54); //周波数軸:□(座標X,座標Y,幅,高さ) 
  for (int xl = 10; xl < 128; xl += 13) {
    u8g2.drawLine(xl, 53, xl, 55); //周波数目盛(始点X座標, 始点Y座標, 終点X座標, 終点Y座標)
  }
  u8g2.setFont(u8g2_font_micro_tr); // Set the font for the numbers
  u8g2.drawStr(10, 64, "1k"); //(X座標, Y座標, 文字列)
  u8g2.drawStr(23, 64, "2k");
  u8g2.drawStr(36, 64, "3k");
  u8g2.drawStr(49, 64, "4k");
  u8g2.drawStr(62, 64, "5k");
  u8g2.drawStr(75, 64, "6k");
  u8g2.drawStr(88, 64, "7k");
  u8g2.drawStr(101, 64, "8k");
  u8g2.drawStr(114, 64, "9k");
}

void loop() {
  for(int i = 0; i < samples; i += 1){  
  frequencySweep(vReal, vImag);
  displayFFT(vReal, vImag); // Call a function to display FFT results.
}
}

0 件のコメント:

コメントを投稿

WaveGene - 多機能 高精度 テスト信号発生ソフト

 WaveGene - 多機能 高精度 テスト信号発生ソフトを使ってみました。  ●サイン波、矩形波、三角波、ノコギリ波、パルス列、パルス列(+-)、ホワイトノイズ、ピンクノイズ、M系列ノイズ(MLS) ●変調(AM、DSB、FM、PM、PWM) ●スイープ(周波数、振幅、位相)...