You are on page 1of 2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%% Generation of Hadamard Matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%% Hadamard matrix of oder N is recursively defined as
%%%%% H(N) =
%%%%% [ H(N/2) H(N/2)
%%%%% [ H(N/2) -H(N/2)]
%%%%%% By definition H(1) = 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Hadamard matrix finds use in signal processing
%%%% One interesting property of Hadamard matrix is
%%%% H(N) * H(N)' = N * I ; I is N*N identity matrix
%%%%%%%%%%%% MATLAB has built-in function hadamard(n)
%%%%% S.R. Bhide
%%%%% 2 April , 2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Uses user created functions as follows:-
%%%%% function [myhada] = my_hadamard(n)
%%%%% function [is_pow2]= my_is_pow2(n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear ,clc
n=8;
hada = my_hadamard(n);
if hada ~=0
fprintf('Hadamard matrix of %d x %d \n',n,n)
hada
(hada*hada')/n
else
fprintf('You asked for Hadamard matrix of size n = %d\n ', n)
fprintf('By definition, Hadamard matrix is n x n where n is power of 2')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [myhada]=my_hadamard(n)
% if (my_is_pow2(n))~= 1
% disp(' argument must be power of 2');
% myhada=0;
% return
% end
% if ( n==1)
% myhada=1;
%
% else
% myhada=[my_hadamard(n/2) my_hadamard(n/2);
% my_hadamard(n/2) -1*my_hadamard(n/2)];
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [is_pow2]=my_is_pow2(n)
% nnn =log2(n);
% nn = round(nnn);
% if ( nnn - nn ) == 0
% is_pow2 = 1 ;
% else
% is_pow2 = 0;
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1
Hadamard matrix of 8 x 8
hada =
1 1 1 1 1 1 1 1
1 -1 1 -1 1 -1 1 -1
1 1 -1 -1 1 1 -1 -1
1 -1 -1 1 1 -1 -1 1
1 1 1 1 -1 -1 -1 -1
1 -1 1 -1 -1 1 -1 1
1 1 -1 -1 -1 -1 1 1
1 -1 -1 1 -1 1 1 -1

ans =
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1

Published with MATLAB® 7.9