derive with fft and ifft

조회 수: 6 (최근 30일)
Rabih Sokhen
Rabih Sokhen 2021년 11월 2일
답변: Satwik 2024년 12월 3일
clear all
clc
z=50;
x=linspace(0,2*pi,z);
a=cos(x);
% derive while using ifft
k=1./x;
tf_a=1i.*k.*fft(a);
da=ifft(tf_a);
plot(x,a,'r',x,da,'b',x,gradient(a)./gradient(x))
legend('cos(x)','ifft(ik(fft(cos(x))))','gradient');
% when I run the program, the amplitude of "da" is different from the amplitude of " gradient(a). /gradient(x)" and it depends on z.
how can I fix this in a way "da "=" gradient(a). /gradient(x)"
thank you in advance

답변 (1개)

Satwik
Satwik 2024년 12월 3일
Hi Rabih,
The discrepancy here might be due to the incorrect handling of the frequency components in the Fourier transform. Here is how you can adjust your code to ensure that the amplitudes match:
clear all
clc
z = 50;
x = linspace(0, 2*pi, z);
a = cos(x);
% Frequency vector
k = [0:z/2-1, -z/2:-1] * (2*pi/(2*pi)); % Adjusted frequency vector
% Fourier transform of the derivative
tf_a = 1i .* k .* fft(a);
da = ifft(tf_a, 'symmetric'); % Use 'symmetric' to avoid complex numerical errors
% Plotting
plot(x, a, 'r', x, da, 'b', x, gradient(a) ./ gradient(x), 'g')
legend('cos(x)', 'ifft(ik(fft(cos(x))))', 'gradient');
The adjustments required are explained below:
1. Frequency Vector: The frequency vector ‘k is constructed to handle both positive and negative frequencies. This ensures that the derivative in the frequency domain is correctly computed.
2. ifft with 'symmetric': Using the 'symmetric' option in ifft ensures that any small imaginary parts due to numerical errors are handled properly, returning a real signal. Please refer to the documentation given below for more information: https://www.mathworks.com/help/matlab/ref/ifft.html.
Below is the output plot generated after the above-mentioned changes:
I hope this adjustment helps you align the amplitude of ‘da’ with ‘gradient(a)./ gradient(x)’ and removes its dependency on ‘z’.

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by