Fs = 1000; % frequency of sampling
T = 1/Fs; % sample time
L = 1000; % length of sampling
t = (0:L-1)*T;

% mix up the signal
frequencies = [1, 120; 0.7, 100; 0.9, 60; 0.2, 250; 0.5, 450]; 
data = frequencies(:,1)'*sin(2*pi*frequencies(:,2)*t);
% 
% data = square(t*100);
% data = sawtooth(t*100);
% data = sin(t*100);
% 
% % half-sine
% for i=1:length(data),
%     if data(i) < 0,
%         data(i) = 0;
%     end
% end

% data = data + 0.5*(1-2*rand(1,L));

subplot(3,1,1);
plot(Fs*t,data);
xlabel('t/ms');
ylabel('intensity');

NFFT = 2^nextpow2(L);
Y = fft(data, NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);

subplot(3,1,2);
plot(f,2*abs(Y(1:NFFT/2+1)));
xlabel('frequency/Hz');
ylabel('|F(\omega)|');

subplot(3,1,3);
recovered = L*ifft(Y);
plot(Fs*t,recovered(1:L),'b', Fs*t,data-recovered(1:L), 'r');
xlabel('t/ms');
ylabel('intensity');


