
derive with fft and ifft
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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 comentarios
Respuestas (1)
  Satwik
      
 el 3 de Dic. de 2024
        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’.
0 comentarios
Ver también
Categorías
				Más información sobre Signal Processing Toolbox en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

