function y=fouriercompress(X,P); % fouriercompress reconstructs an approximation of % of a one-dimensional signal from P percent of its largest % FFT coefficients % X should be a 1xN row vector X=double(X); % floating point subplot(1,2,1); plot(X); axis([1 length(X) min(X) max(X)]); title('original data'); M=length(X); p=(100-P)/(100); % tic % start clock % start clock fX = fft(X); disp(['-----------']); disp('Forward FFT'); toc % stop clock disp(['-----------']); tic fcsort = sort(abs(fX(:))); % sort fourier coeff by magnitudes fcerr = cumsum(fcsort.^2); % sum of squares fcerr = flipud(fcerr); % decreasing order fthresh = fcsort(floor(p*M)); % specify threshold cf_X = fX .* (abs(fX) > fthresh); % keep large disp(['-----------']); disp('Sorting/thresholding'); toc disp(['-----------']); tic icf_X = ifft(cf_X); disp(['-----------']); disp('Inverse FFT'); toc disp(['-----------']); y=real(icf_X); subplot(1,2,2) plot(icf_X); axis([1 length(X) min(X) max(X)]); title('reconstruction from large Fourier coefficients');