2011年2月14日 星期一

程式小記

補零:
int   x=123;
AnsiString   r;
r=r.sprintf( "%010d ",x);

按鍵功能 :KeyPreview = true

2011年1月21日 星期五

VAD紀錄

       THU_energy=mag_max*0.05;                                                // 能量門檻值的上限 *0.05是設定的
        THL_energy=mag_max*0.01;                                                // 能量門檻值的下限 *0.01
        TH_zerocross=max_zer_cross_rate*0.5;               

2011年1月13日 星期四

pinv inv 參考

        对于矩阵A,如果存在一个矩阵B,使得AB=BA=I,其中I为与A,B同维数的单位阵,就称A为可逆矩阵(或者称A可逆),并称B是A的逆矩阵,简称逆阵。(此时的逆称为凯利逆)


矩阵A可逆的充分必要条件是|A|≠0。   非奇异矩阵阵或非方阵的矩阵不存在逆矩阵,但可以用函数pinv(A)求其伪逆矩阵。基本语法 为X=pinv(A),X=pinv(A,tol),其中tol为误差:max(size(A))*norm(A)*eps。函数返回一个与A的转置矩阵 A' 同型的矩阵X,并且满足:AXA=A,XAX=X.此时,称矩阵X为矩阵A的伪逆,也称为广义逆矩阵。pinv(A)具有inv(A)的部分特性,但不与 inv(A)完全等同。   

        如果A为非奇异方阵,pinv(A)=inv(A),但却会耗费大量的计算时间,相比较而言,inv(A)花费更少的时间。

2011年1月11日 星期二

normalize 目前

coordinate[i][0][p]=(float)(coordinate[i][0][p]-minx)/(float)(maxx-minx)*150+85;
coordinate[i][1][p]=(float)(coordinate[i][1][p]-miny)/(float)(maxy-miny)*150+45;

2011年1月6日 星期四

IEEE754 convert

pow(0.5,127)*N*2.3841858E-7

N為10進位數值
即可算出
http://www.h-schmidt.net/FloatApplet/IEEE754.html
最下面的數

HTK file
 voice:每秒100sample點
image:每秒30sample點

2011年1月5日 星期三

[6] vs2005c++專案屬性字元集

傳入的參數字串變成亂碼,為何會如此?

原因是因為vs2005c++專案屬性字元集預設為使用unicode字元集。

而我們在『命令提示字元』視窗中輸入的並非unicode字元集而是多位元組字集(中文系統的關係),所以ex2.exe所接受的字串參數無法正確顯示,

我們只要將專案的一般屬性中字元集屬性改成未設定或使用多位元組字集,重新編譯專案,即可解決此問題。

2010年12月15日 星期三

PCA eigenface

 出處:http://www.pages.drexel.edu/~sis26/Eigencode.htm

% read and show image
M=4;

% Chosen std and mean.
% It can be any number that it is close to the std and mean of most of the images.
um=100;
ustd=80;

S=[];    % img matrix
figure(1);
for i=1:M
    str=strcat(int2str(i),'fr','.bmp');    % concatenates two strings that form the name of the image
    eval('img=rgb2gray(imread(str));');
    subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
    imshow(img)
    if i==3
        title('Training set','fontsize',18)
    end
drawnow;
[irow icol]=size(img);    % get the number of rows (N1) and columns (N2)
temp=reshape(img',irow*icol,1);    % creates a (N1*N2)x1 vector
S=[S temp];    % S is a N1*N2xM matrix after finishing the sequence
end

% Here we change the mean and std of all images. We normalize all images.
% This is done to reduce the error due to lighting conditions and background.
for i=1:size(S,2)
    temp=double(S(:,i));
    m=mean(temp);
    st=std(temp);
    S(:,i)=(temp-m)*ustd/st+um;
end

% show normalized images
figure(2);
for i=1:M
    str=strcat(int2str(i),'.jpg');
    img=reshape(S(:,i),icol,irow);
    img=img';
    eval('imwrite(img,str)');
    subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
    imshow(img)
    drawnow;
    if i==3
        title('Normalized Training Set','fontsize',18)
    end
end

% mean image
m=mean(S,2);  % obtains the mean of each row instead of each column
tmimg=uint8(m); % converts to unsigned 8-bit integer. Values range from 0 to 255
img=reshape(tmimg,icol,irow); % takes the N1*N2x1 vector and creates a N1xN2 matrix
img=img';
figure(3);
imshow(img);
title('Mean Image','fontsize',18)


% Change image for manipulation
dbx=[];    % A matrix
for i=1:M
    temp=double(S(:,i));
    dbx=[dbx temp];
end

%Covariance matrix C=A'A, L=AA'
A=dbx';
L=A*A';
% vv are the eigenvector for L
% dd are the eigenvalue for both L=dbx'*dbx and C=dbx*dbx';
[vv dd]=eig(L);
% Sort and eliminate those whose eigenvalue is zero
v=[];
d=[];
for i=1:size(vv,2)
    if(dd(i,i)>1e-4)
        v=[v vv(:,i)];
        d=[d dd(i,i)];
    end
end

%sort, will return an ascending sequence
[B index]=sort(d);
ind=zeros(size(index));
dtemp=zeros(size(index));
vtemp=zeros(size(v));
len=length(index);
for i=1:len
    dtemp(i)=B(len+1-i);
    ind(i)=len+1-index(i);
    vtemp(:,ind(i))=v(:,i);
end
d=dtemp;
v=vtemp;

%Normalization of eigenvectors
for i=1:size(v,2) %access each column
    kk=v(:,i);
    temp=sqrt(sum(kk.^2));
    v(:,i)=v(:,i)./temp;
end

%Eigenvectors of C matrix
u=[];
for i=1:size(v,2)
    temp=sqrt(d(i));
    u=[u (dbx*v(:,i))./temp];
end

%Normalization of eigenvectors
for i=1:size(u,2)
    kk=u(:,i);
    temp=sqrt(sum(kk.^2));
    u(:,i)=u(:,i)./temp;
end

% show eigenfaces
figure(4);
for i=1:size(u,2)
    img=reshape(u(:,i),icol,irow);
    img=img';
    img=histeq(img,255);
    subplot(ceil(sqrt(M)),ceil(sqrt(M)),i)
    imshow(img)
    drawnow;
    if i==3
        title('Eigenfaces','fontsize',18)
    end
end

% Find the weight of each face in the training set
omega = [];
for h=1:size(dbx,2)
    WW=[];
    for i=1:size(u,2)
        t = u(:,i)';
        WeightOfImage = dot(t,dbx(:,h)');
        WW = [WW; WeightOfImage];
    end
    omega = [omega WW];
end

% Acquire new image
% Note: the input image must have a bmp or jpg extension.
% It should have the same size as the ones in your training set.
% It should be placed on your desktop
InputImage = input('Please enter the name of the image and its extension \n','s');
InputImage = rgb2gray(imread(InputImage));
figure(5)
subplot(1,2,1)
imshow(InputImage); colormap('gray');title('Input image','fontsize',18)
InImage=reshape(double(InputImage)',irow*icol,1);
temp=InImage;
me=mean(temp);
st=std(temp);
temp=(temp-me)*ustd/st+um;
NormImage = temp;
Difference = temp-m;

p = [];
aa=size(u,2);
for i = 1:aa
    pare = dot(NormImage,u(:,i));
    p = [p; pare];
end
ReshapedImage = m + u(:,1:aa)*p; %m is the mean image, u is the eigenvector
ReshapedImage = reshape(ReshapedImage,icol,irow);
ReshapedImage = ReshapedImage';
%show the reconstructed image.
subplot(1,2,2)
imagesc(ReshapedImage); colormap('gray');
title('Reconstructed image','fontsize',18)

InImWeight = [];
for i=1:size(u,2)
t = u(:,i)';
WeightOfInputImage = dot(t,Difference');
InImWeight = [InImWeight; WeightOfInputImage];
end

ll = 1:M;
figure(68)
subplot(1,2,1)
stem(ll,InImWeight)
title('Weight of Input Face','fontsize',14)

% Find Euclidean distance
e=[];
for i=1:size(omega,2)
q = omega(:,i);
DiffWeight = InImWeight-q;
mag = norm(DiffWeight);
e = [e mag];
end

kk = 1:size(e,2);
subplot(1,2,2)
stem(kk,e)
title('Eucledian distance of input image','fontsize',14)

MaximumValue=max(e)  % maximum eucledian distance
MinimumValue=min(e)    % minimum eucledian distance