# getNaturalStat_multiscale

## PURPOSE

getNaturalStat_multiscale - Obtain natural statistics for multiscale Gabor responses

## SYNOPSIS

function getNaturalStat_multiscale

## DESCRIPTION

     getNaturalStat_multiscale - Obtain natural statistics for multiscale Gabor responses

## CROSS-REFERENCE INFORMATION

This function calls:

- GaborFilter GaborFilter - generate Gabor filter at fixed scale and orientation.

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 function getNaturalStat_multiscale
    0002 % getNaturalStat_multiscale - Obtain natural statistics for multiscale Gabor responses
    0003
    0004 load ../template_params.mat
    0005 negImageNames = dir('../image/neg/*.jpg');
    0006 negImageNames = negImageNames(1:min(5,end));
    0007 numImage = length(negImageNames);
    0008 mkdir('../result');
    0009
    0010 for s = 1 : nGaborScale
    0011     scaleFilter = GaborScale(s);
    0012     %% compute histogram of q()
    0013     numBin = floor(GaborSaturation/binSize)+1;  % binnumbers
    0014     histog = zeros(numBin, 1, 'single');  % store F
    0015     r = (0:(numBin-1))*binSize;
    0016     halfFilterSize = ( size(GaborFilter{nGaborOri*(s-1)+1},1)-1 ) / 2;
    0017     disp(['start histogramming negative images at Gabor length ' num2str(2*halfFilterSize+1)]);
    0018
    0019     allFilteredImage = cell(length(negImageNames), nGaborOri);
    0020     sizex = zeros(numImage,1,'int32');
    0021     sizey = zeros(numImage,1,'int32');
    0022     for i = 1:numImage
    0023         load(sprintf('../feature/neg/%s.mat',negImageNames(i).name),'S1Map');
    0024         allFilteredImage(i,:) = S1Map(nGaborOri*(s-1)+(1:nGaborOri));
    0025         sizex(i) = size(allFilteredImage{i,1},1);
    0026         sizey(i) = size(allFilteredImage{i,1},2);
    0027     end
    0028
    0029     Chistogram(numImage, nGaborOri, allFilteredImage, halfFilterSize, sizex, sizey, binSize, numBin, histog, GaborSaturation);
    0030     %% compute stored lambda, expectation, logZ
    0031
    0032     storedExpectation = zeros(numStoredPoint, 1);
    0033     Z = zeros(numStoredPoint, 1);
    0034     for k=1:numStoredPoint
    0035        lambda = (k-1.)*spacing;
    0036        p = exp(lambda*r).*(histog');
    0037        Z(k) = sum(p*binSize); p = p/Z(k);
    0038        storedExpectation(k) = sum(r.*p*binSize);
    0039     end
    0040     storedLambda = (0:(numStoredPoint-1))*spacing;
    0041     storedLogZ = log(Z);
    0042
    0043     %% store filter properties and exponential model
    0044     storeName = ['../result/naturalStat' num2str(s-1) '.mat'];
    0045     if SinglePrecision > 0
    0046         storedLambda = single(storedLambda);
    0047         storedExpectation = single(storedExpectation);
    0048         storedLogZ = single(storedLogZ);
    0049         r = single(r);
    0050         histog = single(histog);
    0051     end
    0052     save(storeName, 'storedLambda', 'storedExpectation', 'storedLogZ', 'numBin', 'r', 'histog');
    0053 end
    0054
    0055 %% display the comparison
    0056 rMat = zeros(nGaborScale,length(r));
    0057 histogMat = zeros(nGaborScale,length(r));
    0058 for s = [1 4 7]
    0059     load(sprintf('../result/naturalStat%d.mat',s-1),'r','histog');
    0060     rMat(s,:) = r(:);
    0061     histogMat(s,:) = histog(:);
    0062 end
    0063
    0064 figure; plot(rMat([1 4 7],:)',histogMat([1 4 7],:)');
    0065 legend({'1','4','7'});
    0066 title('comparison of background density'); xlabel('sigmoid(response)'); ylabel('density');
    0067 set(gca,'FontSize',16); % adjust the display format and save the plot
    0068 set(get(gca,'XLabel'),'FontSize',18);
    0069 set(get(gca,'YLabel'),'FontSize',18);
    0070 set(get(gca,'Title'),'FontSize',18);
    0071 set(gcf, 'PaperPositionMode', 'auto');
    0072 print('-dpng','../result/background_density_comparison_over_scale.png');
    0073 saveas(gcf, '../result/background_density_comparison_over_scale.eps', 'epsc');
    0074
    0075 lambdaMat = zeros(nGaborScale,length(storedLambda));
    0076 expMat = zeros(nGaborScale,length(storedLambda));
    0077 for s = [1 4 7]
    0078     load(sprintf('../result/naturalStat%d.mat',s-1),'storedLambda','storedExpectation');
    0079     lambdaMat(s,:) = storedLambda(:);
    0080     expMat(s,:) = storedExpectation(:);
    0081 end
    0082
    0083 figure; plot(lambdaMat([1 4 7],:)',expMat([1 4 7],:)');
    0084 title('comparison of lambda-expectation relation'); xlabel('lambda'); ylabel('expected value');
    0085 legend({'1','4','7'},'Location','South');
    0086 set(gca,'FontSize',16); % adjust the display format and save the plot
    0087 set(get(gca,'XLabel'),'FontSize',18);
    0088 set(get(gca,'YLabel'),'FontSize',18);
    0089 set(get(gca,'Title'),'FontSize',18);
    0090 set(gcf, 'PaperPositionMode', 'auto');
    0091 print('-dpng','../result/lambda_exp_comparison_over_scale.png');
    0092 saveas(gcf, '../result/lambda_exp_comparison_over_scale.eps', 'epsc');