2025年10月1日水曜日

Pico FFT - FIR.h 特性確認

Raspberry Pi Picoによる FIRフィルタ 係数31タップ(カットオフ周波数3kHz)を下記サイトのライブラリ#include <FIR.h>を使用してFIRフィルタの周波数特性確認を行いました。 
     




参考サイト
●FIRフィルタArduinoライブラリ【https://github.com/LeemanGeophysicalLLC/FIR_Filter_Arduino_Library
●github.com/JR3XNW


結線
OLED  Pico
SDA   -   GP16
SCL   -   GP17
VCC   -   3V3
GND   -   GND




係数計算(参考)



タップ数とFIRリスト貼付
 float coef_lp[17] = {
0.000477,
0.003621,
0.003020,
-0.014864,
-0.038303,
-0.011437,
0.109452,
0.272446,
0.350000,
0.272446,
0.109452,
-0.011437,
-0.038303,
-0.014864,
0.003020,
0.003621,
0.000477};




プログラム  Arduino IDE【ボード:Raspberry Pi Pico】
FIRフィルタ 係数31タップ(カットオフ周波数3kHz)
#include <Arduino.h>
#include <U8g2lib.h>
#include <arduinoFFT.h> // v2.0.2
#include <Wire.h>
#include <FIR.h>

FIR<float, 31> fir_lp; //FIRローパスフィルタとタップ数指定

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.
const float startFrequency = 100.0;  // Start frequency (Hz)
const float endFrequency = 10000.0;  // End frequency (Hz)

// frequency sweep function.
void frequencySweep(double *vReal, double *vImag) {
  float frequencyStep = (endFrequency - startFrequency) / samples;
  for (int i = 0; i < samples; i++) {
    float currentFrequency = startFrequency + (i * frequencyStep);
    //sin波 sin*2*π*f*t
    float signal = sin(2 * PI * currentFrequency * millis() / 256.0);
    float filteredSignal = fir_lp.processReading(signal); //sweep(signal)→firフィルタ
    //float filteredSignal = signal/2; //sweep信号のみ
    vReal[i] = filteredSignal;
    vImag[i] = 0;
  }
}

// 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).

  // Drawing FFT results.
  for (int i = 1; i < (samples / 2); i++) {
    int x = map(i, 1, samples / 2, 0, 128); //周波数軸(現数値,現下限,現上限,変換下限,変換上限)
    int y = map(vReal[i], 0, 14, 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");
 
 //FIR Cutoff-3KHz LPF
 //https://github.com/JR3XNW/pico-Program-Lab/blob/main/Filter_sweep_test_SpectrumDisplay_FIR.ino
 //FIRタップ数と係数  
  float coef_lp[31] = {0.001695,0.001201,-0.000905,-0.004228,-0.005427,0.0,
   0.011365,0.018584,0.00825,-0.021236,-0.048939,-0.03959,
   0.029858,0.145107,0.254511,0.299507,0.254511,0.145107,
   0.029858,-0.03959,-0.048939,-0.021236,0.00825,0.018584,
   0.011365,0.0,-0.005427,-0.004228,-0.000905,0.001201,0.001695};
   
  //Set the coefficients 係数セット
  fir_lp.setFilterCoeffs(coef_lp);
 }
void loop() {
  double vReal[samples];
  double vImag[samples] = {0}; // Imaginary part is initialized to 0.

  frequencySweep(vReal, vImag);
  displayFFT(vReal, vImag); // Call a function to display FFT results.
}

0 件のコメント:

コメントを投稿

Pico SDR - FIR・IIRフィルタ 受信テスト

github.com/JR3XNW FIR・IIRフイルタ及びライブラリFIR・IIRフイルタを使用しRaspberry Pi Picoによる、Si5351A 7MHz VFO→直交ミキサ→ADC入力→SSB検波【demod = adc_I - adc_Q】→FIR・IIRフィル...