Мне надо использовать библиотеку FFTW. Сайт с примером. Я написал код, основанный на примере с сайта. Мне надо с помощью БПФ получить АЧХ синусоидального сигнала. Входные данные - комплексные значения. Поэтому используется массивы
double[] fin, fout;
в которых fin[i*2] - действительное число, fin[i * 2 + 1] - мнимое число. Но как я понял т.к. у меня синусоидальный сигнал то мнимая часть должна быть равна нулю, т.е. fin[i * 2 + 1] = 0
Я написал следующий код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using fftwlib;
namespace fure
{
public class fftwtest
{
//pointers to unmanaged arrays
IntPtr pin, pout;
//managed arrays
double[] fin, fout;
//pointers to the FFTW plan objects
public IntPtr fplan1, fplan2;
// Initializes FFTW and all arrays
// n: Logical size of the transform
public void InitFFTW(int n)
{
//create two unmanaged arrays, properly aligned
pin = fftwf.malloc(n * 8);
pout = fftwf.malloc(n * 8);
//create two managed arrays, possibly misalinged
//n*2 because we are dealing with complex numbers
fin = new double[n*2];
fout = new double[n*2];
double T = 0.0000159;//длительность сигнала
double F = 345000; //частота сигнала
double F_d = 6250000;//частота дискретизации
n = (int)(T * F_d);
//fill our arrays with a sawtooth signal
for (int i = 0; i < n; i++)
{
fin[i*2] = 5 * Math.Sin(2 * Math.PI * F * i/F_d);
fin[i * 2 + 1] = 0;
}
//copy managed arrays to unmanaged arrays
Marshal.Copy(fin, 0, pin, n*2 );
//create a few test transforms
fplan1 = fftwf.dft_1d(n, pin, pout, fftw_direction.Forward, fftw_flags.Estimate);
}
// Tests a single plan, displaying results
//plan: Pointer to plan to test
public void TestPlan(IntPtr plan, int n)
{
fftwf.execute(plan);
Marshal.Copy(pout, fout, 0, n * 2);
Console.WriteLine(fout[30]);
}
// Releases all memory used by FFTW/C#
public void FreeFFTW()
{
//it is essential that you call these after finishing
//may want to put the initializers in the constructor
//and these in the destructor
fftwf.free(pin);
fftwf.free(pout);
fftwf.destroy_plan(fplan1);
}
}
class Program
{
static void Main(string[] args)
{
double T = 0.0000159;//длительность сигнала
double F_d = 6250000;//частота дискретизации
int n = (int)(T * F_d);
fftwtest test = new fftwtest();
test.InitFFTW(n);
test.TestPlan(test.fplan1, n);
Console.ReadLine();
}
}
}
Выходной массив fout. Но в этом массиве получаются какие то огромные значения, при возведении которых в квадрат получается бесконечность.
Где может быть ошибка?