![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1817033/image.png)
derive with fft and ifft
6 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1817033/image.png)
I hope this adjustment helps you align the amplitude of ‘da’ with ‘gradient(a)./ gradient(x)’ and removes its dependency on ‘z’.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!