MATLAB - 傅立葉變換



傅立葉變換是一種強大的數學工具,用於訊號處理、影像處理和許多其他領域。它將時間函式(或訊號)分解成其組成頻率。在 MATLAB 中,可以使用 fft 函式計算一維訊號的傅立葉變換,或使用 fft2 函式計算二維訊號(如影像)的傅立葉變換。

傅立葉變換有許多應用,例如訊號和影像的濾波、壓縮和分析。瞭解傅立葉變換有助於分析和處理 MATLAB 中各種型別的資料。

使用 fourier() 函式進行傅立葉變換

MATLAB 中的 fourier 函式用於計算週期訊號的傅立葉級數係數或非週期訊號的傅立葉變換。它可以用於分析時域和頻域中訊號的頻率成分。該函式返回復係數,表示訊號在不同頻率下正弦分量的幅度和相位。

語法

fourier(f)
fourier(f,transVar)
fourier(f,var,transVar)

語法解釋

fourier(f) − 計算函式 f 的傅立葉變換。它使用 symvar 查詢自變數,w 是用於變換的變數。

fourier(f,transVar) − 計算函式 f 的傅立葉變換,使用變數 transVar 代替 w 進行變換。

fourier(f,var,transVar) − 計算函式 f 的傅立葉變換。它分別使用 var 作為自變數和 transVar 作為變換變數,而不是預設的 symvar 和 w。

讓我們根據上面解釋的語法看幾個例子。

示例 1:計算函式的傅立葉變換

我們擁有的程式碼為 -

syms t w
f = exp(-t^2); % Define the function
F = fourier(f); % Calculate the Fourier Transform
disp(F); % Display the result
  • 在此示例中,我們有一個定義為 exp(-t^2) 的函式 f。
  • 我們使用 fourier 函式計算 f 的傅立葉變換。
  • 函式 fourier 使用 symvar 將 t 確定為自變數,將 w 確定為變換變數。
  • 結果 F 是函式 f 的傅立葉變換,使用 disp(F) 顯示。

執行後輸出為 -

pi^(1/2)*exp(-w^2/4)

示例 2:使用自定義變換變數計算傅立葉變換

我們擁有的程式碼為 -

syms t u
f = sin(2*pi*t); % Define the function
F = fourier(f, u); % Calculate the Fourier Transform with 
   u as the transformation variable
disp(F); % Display the result
  • 在此示例中,我們有一個定義為 sin(2*pi*t) 的函式 f。
  • 我們使用 fourier 函式,並將變數 u 指定為變換變數,而不是預設的 w。
  • 結果 F 是函式 f 相對於變數 u 的傅立葉變換。
  • 結果使用 disp(F) 顯示。

執行後輸出為 -

-pi*(dirac(u - 2*pi) - dirac(u + 2*pi))*1i

示例 3:使用自定義變數進行傅立葉變換

我們擁有的程式碼為 -

syms t omega
f = sin(omega*t); % Define the function
F = fourier(f, t, omega); % Calculate the Fourier Transform with t as 
   the independent variable and omega as the transformation variable
disp(F); % Display the result

我們獲得的輸出為 -

>> syms t omega
f = sin(omega*t); % Define the function
F = fourier(f, t, omega); % Calculate the Fourier Transform with t as 
   the independent variable and omega as the transformation variable
disp(F); % Display the result

-1i*Inf
 
>> 

使用 fft() 函式進行傅立葉變換

傅立葉變換是一種數學技術,可將訊號分解成其組成頻率。在 MATLAB 中,可以使用 fft 函式計算一維訊號的傅立葉變換。

語法

Y = fft(X)
Y = fft(X,n)
Y = fft(X,n,dim)

語法解釋

Y = fft(X) − 使用稱為快速傅立葉變換 (FFT) 的快速演算法計算 X 的離散傅立葉變換 (DFT)。輸出 Y 與 X 大小相同。

  • 如果 X 是向量,則 fft(X) 返回向量的傅立葉變換。
  • 如果 X 是矩陣,則 fft(X) 將 X 的列視為向量並返回每列的傅立葉變換。
  • 如果 X 是多維陣列,則 fft(X) 將第一個大小不為 1 的陣列維度上的值視為向量並返回每個向量的傅立葉變換。

Y = fft(X,n) − 計算 X 的 n 點離散傅立葉變換 (DFT)。

  • 如果 X 是向量且短於 n,則將零新增到 X 的末尾以使其長度為 n。
  • 如果 X 是向量且長於 n,則 X 將縮短為長度 n。
  • 如果 X 是矩陣,則每列都像向量一樣處理。
  • 如果 X 是多維陣列,則第一個大小不為 1 的維度將像向量一樣處理。

Y = fft(X,n,dim) − 沿 X 的指定維度 dim 計算傅立葉變換。

  • 如果 X 是矩陣,則 fft(X,n,2) 計算每行的 n 點傅立葉變換。
  • 如果 X 是多維陣列,則沿指定的維度 dim 計算傅立葉變換。

讓我們根據上面解釋的語法看幾個例子。

示例 1:計算離散傅立葉變換 (DFT)

我們擁有的程式碼為 -

% Create a simple signal
x = [1, 2, 3, 4];

% Calculate the Discrete Fourier Transform (DFT) of the signal
y = fft(x);

% Display the original signal and its DFT
disp('Original Signal:');
disp(x);

disp('Discrete Fourier Transform:');
disp(y);
  • 在此示例中,我們有一個簡單的一維訊號 x,包含四個值 [1, 2, 3, 4]。
  • 我們使用 fft 函式計算訊號的離散傅立葉變換 (DFT),並將結果儲存在 y 中。
  • fft 函式使用稱為快速傅立葉變換 (FFT) 的快速演算法計算 DFT。
  • 輸出 y 與輸入訊號 x 大小相同,包含訊號的傅立葉變換。

執行後,我們得到的輸出如下所示 -

Original Signal:
     1     2     3     4

Discrete Fourier Transform:
  10.0000 + 0.0000i  -2.0000 + 2.0000i  -2.0000 + 0.0000i  -2.0000 - 2.0000i

示例 2:計算 n 點離散傅立葉變換 (DFT)

我們擁有的程式碼為 -

% Create a simple signal
x = [1, 2, 3, 4];

% Calculate the 8-point Discrete Fourier Transform (DFT) of the signal
y = fft(x, 8);

% Display the original signal and its DFT
disp('Original Signal:');
disp(x);

disp('8-point Discrete Fourier Transform:');
disp(y);
  • 在此示例中,我們有一個簡單的一維訊號 x,包含四個值 [1, 2, 3, 4]。
  • 我們使用 fft 函式以及引數 8 來計算訊號的 8 點離散傅立葉變換 (DFT),並將結果儲存在 y 中。
  • 由於 x 的長度小於 8,因此將零新增到 x 的末尾以使其長度為 8。
  • 輸出 y 是訊號的 8 點 DFT。

執行後,我得到的輸出為 -

Original Signal:
   1   2   3   4
8-point Discrete Fourier Transform:
 Columns 1 through 3:

   10.0000 +       0i   -0.4142 -  7.2426i   -2.0000 +  2.0000i

 Columns 4 through 6:

    2.4142 -  1.2426i   -2.0000 +       0i    2.4142 +  1.2426i

 Columns 7 and 8:

   -2.0000 -  2.0000i   -0.4142 +  7.2426i

示例 3:沿指定維度計算傅立葉變換

我們擁有的程式碼如下所示 -

% Create a 2x4 matrix
X = [1, 2, 3, 4; 5, 6, 7, 8];

% Compute the Fourier transform along the second dimension (columns)
Y = fft(X, [], 2);

% Display the original matrix and its Fourier transform
disp('Original Matrix:');
disp(X);

disp('Fourier Transform Along Columns:');
disp(Y);
  • 在此示例中,我們有一個具有兩行和四列的 2x4 矩陣 X。
  • 我們使用帶有引數 2 的 fft 函式來計算 X 的第二維(列)上的傅立葉變換,並將結果儲存在 Y 中。
  • X 的每一列都被視為一個向量,並且獨立計算每一列的傅立葉變換。
  • 輸出 Y 是一個 2x4 矩陣,包含 X 的各列的傅立葉變換。

執行後,我們得到的輸出如下所示 -

Original Matrix:
   1   2   3   4
   5   6   7   8
Fourier Transform Along Columns:
   10 +  0i   -2 +  2i   -2 +  0i   -2 -  2i
   26 +  0i   -2 +  2i   -2 +  0i   -2 -  2i
廣告

© . All rights reserved.