Firstly, I try to use ifft to convert displacement frequency into displacement time but the ifft fails to give the original sign.The code as below:
close all; clear all; clc
t = 0:0.01:1;
x= 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
s = x + 2*randn(size(t)); % Sinusoids plus noise
plot(t,s)
xlabel('Time(s)')
ylabel('Displacement(m)')
dt = 0.01; % time interval
y1 = fft(s); % perform fft
N = length(y1)-1;
dy=y1;
y = dy*2/N; % calculate actual amplitude
%the y need to x2,because it become half
f = (0:N)/(N*dt); % calculate actual frequency
figure, plot(f(1:N/2),y(1:N/2));
xlabel('Frequency(Hz)')
ylabel('Displacement(m)')
[m,n]=size(dy);
for j=1:n;
for i=1:m;
if dy(i,j)>1;
dy(i,j)=dy(i,j);
else
dy(i,j)=0;
end
end
end % set numbers < 1 to zero
IDFT = ifft(dy); % inverse Fourier transform of fixed data
cy2 = real(IDFT);
df=f(2)-f(1);%sample spacing in frequency
total_t=1/df; % %sample spacing in time
T=total_t*(0:N)/N;% build the time axis from zero the the Nyquist frequenc
figure,plot(T(1:N),cy2(1:N)); grid on % plot corrected signal
xlabel('Time (s)','fontsize',20,'fontname','cambria'); % time expressed in seconds
ylabel('Displacement(m)','fontsize',20,'fontname','cambria')
hold on;plot(t,s,'red');
What goes wrong?