2015-01-09 192 views
1

即時使用LomontFFT算法的C#。在Matlab代碼如下:將fft代碼從Matlab轉換爲C#

idx = 3000 : 5048; 
figure  
hold on 
plot(abs(fft(a_eeg1_raw(idx))), 'y') 
%Title,xlabel,ylabel etc 

所以,我想我最好在C#中做同樣的:

int idx_start = 3000 + 1; 
int idx_end = 5048; 
LomontFFT my_fft = new LomontFFT(); 
double[] temp = new double[idx_end - idx_start + 1]; 
for (int i = idx_start, j = 0; i < idx_end + 1; i++, j++) 
    temp[j] = a_eeg1_raw[i]; 
my_fft.RealFFT(temp, true); // With (A,B) (1,-1) - signal processing 
for (int i = 3, j = 1; i < temp.Count() - 1; i = i + 2, j++) // as long as first to entries have only real parts I'm starting from 3rd entry (imaginary part) 
    series_a_eeg1_fft.Points.Add(new DataPoint(Convert.ToDouble(j), Math.Abs(temp[i]))); 
for (int i = temp.Count() - 1, j = seria_a_eeg1_raw.Points.Count() - 1; i > 2; i = i - 2, j++) 
    series_a_eeg1_fft.Points.Add(new DataPoint(Convert.ToDouble(j), Math.Abs(temp[i]))); 
model_tab1.PlotModel.Series.Add(series_a_eeg1_fft); 

,我已經從這個代碼得到的值比那些非常不同Matlab的。情節的形狀沒有太大的不同,它不一樣。我很確定我忘記了一些重要的事情,但是在3小時後,我看不出這是什麼錯誤。有人能幫助我嗎?

回答

1

abs(fft(x))abs(real(fft(x)))不一樣。你沒有正確地結合真實和想象的部分。

假設this is the documentation

計算正向或傅立葉逆變換的數據,與僅包含實值數據
數據。 輸出是複雜的
在前兩個條目之後被賦值,存儲在交替的實數
和虛數部分
中。前兩個返回的條目是真正的
部分從共軛對稱
輸出,這必然是真正的

+0

感謝第一個和最後一個值,即解決我的問題非常好,對了解一下腹肌即時閱讀MATLAB。 – Finchsize 2015-01-09 21:27:48

+0

@Finchsize:一個複數的'abs'只是畢達哥拉斯定理:'sqrt(real * real + imaginary * imaginary)' – 2015-01-09 21:29:27