%To study the aliasing effect when using DFT to interpolate the sin(2*pix) %on [0,1] clear all;close all; N=16; %Number of points used to interpolate xinterp=linspace(0,1-1/N,N); %Points used to interpolate f xvis=linspace(0,1,500); %Points used to visualize f %f is a function. The command f(x) returns the value of f at x. %If x is an array of numbers (like that one generated by linspace), then %f(x) returns an array of numbers each one representing the value of f %computed at the corresponding entry of the vector x. f=@(x) sin(2*pi*x); %Let's interpolate the sine function fvis=f(xvis); %values of f computed at the points xvis finterp=f(xinterp); %values of f computed at the points xinterp fhat=FT_bf(-1,finterp); %Compute DFT coefficients using brute force approach %optioninterp can be either 'OptionA' or 'OptionB' optioninterp='OptionA'; %Reconstruct function on xvis using Fourier coefficients fhat fspectral=zeros(1,length(xvis)); for k=1:N switch optioninterp case 'OptionA' %-Optiona A: THIS DOES NOT WORK fspectral=fspectral+fhat(k)*exp(2*pi*1i*(k-1)*xvis); case 'OptionB' %-Option B: THIS WORK if k<= N/2; fspectral=fspectral+fhat(k)*exp(2*pi*1i*(k-1)*xvis); end; if k>N/2 ; fspectral=fspectral+fhat(k)*exp(2*pi*1i*((k-1)-N)*xvis); end; end end %-The curve in red represents f evaluated at the points xvis %-The blue curve represents the trigonometric interpolation of the function %f obtained using the coefficients fhat. %-The blue circles represents the value of f used to build the %interpolation. %-When using the OptionB the two curves cannot be distinguish (they %overlap). When using OptionA the values of the interpolation are very %different from the values of the function f. Why it is so? figure plot(xvis,fvis,'r-') hold on; plot(xvis,real(fspectral),'-b') plot(xinterp,finterp,'o') title('Sine function (red)-Interpolated function (blue)')