APPENDIX C

MATLAB ROUTINES

C.1 GENERAL COMPRESSION ROUTINE

The following Matlab function compresses a given vector, thereby zeroing out the terms falling below a user specified (percentage) threshold. This routine is used by all fft and wavelet compression schemes that follow.

function wc=compress(w,r)
% Input is the array w and r, which is a number
% between 0 and 1.
% Output is the array wc where smallest 100r% of the
% terms in w are set to zero (e.g r=.75 means the smallest 75% of
% the terms are set to zero
if (r<0) | (r>1)
  error(’r should be between 0 and 1’) end;
N=length(w); Nr=floor(N*r); ww=sort(abs(w)); tol=abs(ww(Nr)); for j=1:N
  if (abs(w(j)) < tol)
w(j)=0;
  end;
end;
wc=w;

C.2 USE OF MATLAB’S FFT ROUTINE FOR FILTERING AND COMPRESSION

Filtering with FFT. The following Matlab commands are needed for Example 3.6, which discusses filtering the high-frequency components from a given signal. The key Matlab commands are fft and ifft, which compute the fast Fourier transform and its inverse, respectively.

 >> t=linspace(0,2*pi,2^8); % discretizes [0, 2pi] into 256 nodes >> y=exp(-(cos(t).^2)).*(sin(2*t)+2*cos(4*t)+0.4*sin(t).*sin(50*t)); >> plot(t,y) % generates the graph of the original signal >> fy=fft(y); % computes fft of y >> filterfy=[fy(1:6) zeros(1,2^8-12) fy(2^8-5:2^8)]; % sets fft coefficients >>% to zero for $7 \leq k \leq 256$ >> filtery=ifft(filterfy); % computes inverse fft of the filtered fft >> plot(t, filtery) % generates the plot of the compressed signal ...

Get A First Course in Wavelets with Fourier Analysis, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.