Page 137 - Algorithm Collections for Digital Signal Processing Applications using MATLAB
P. 137

3. Numerical Linear Algebra                                      125

           14.1.2   M-file for haar forward and inverse transformation

              ___________________________________________________________

              haartrans.m

              i=0:1/511:1;
              a=sin(2*pi*i)+sin(2*pi*100*i);
              plot(a)
              figure
              s=length(i);
              for i=1:1:log2(s)
                  temp=createhaarmatrix(length(a))*a';
                  temp=reshape(temp,2,length(temp)/2);
                  approx{i}=temp(1,:);
                  det{i}=temp(2,:);
                  a=approx{i};
              end
              for k=1:1:(length(approx)-1)
              subplot((length(approx)-1),1,k)
              plot(approx{k})
              title(strcat('approx',num2str(k)))
              end
              subplot(
              figure
              for k=1:1:(length(approx)-1)
              subplot((length(approx)-1),1,k)
              plot(det{k})
              title(strcat('detail',num2str(k)))
              end
              ___________________________________________________________
              createhaarmatrix.m

              function [res]=createhaarmatrix(m)
              temp1=[ones(1,m/2);(-1)*ones(1,m/2)];
              temp1=reshape(temp1,1,size(temp1,1)*size(temp1,2));
              d1=temp1.*(1/2);
              temp2=[ones(1,m/2);zeros(1,m/2)];
              temp2=reshape(temp2,1,size(temp2,1)*size(temp2,2));
              d2=(1/2)*temp2(1:1:length(temp2)-1);
              res=diag(d1)+diag(d2,1)+diag(d2,-1);
   132   133   134   135   136   137   138   139   140   141   142