function ex9_1()
    % "Significance" of an artifact (in an otherwise-noisy background)
    Initialize();
    X = getAllPatterns();
    [nPatterns, nPoints] = size(X);
    Xav = sum(X, 1);
    corrCoeffs = randn(1, nPoints);
    for cntr = 1:nPatterns
        corrCoeffs(cntr) = getCorrCoeff(Xav, X(cntr,:));
    end
    figure;
    subplot(2, 2, 1);
    imagesc(X);
    axis tight;
    subplot(2, 2, 3);
    plot(Xav);
    box on;
    axis tight;
    title('Average data');
    subplot(1, 2, 2);
    stem(corrCoeffs);
    title(['Mean(|Corr coeff|) = ' num2str(mean(abs(corrCoeffs)))]);
end
function X = getAllPatterns()
    if (menu('Source of data', 'File', 'Simulation') == 1)
        filePath = uigetfile('*.*','Please select the data file');
        X = importdata(filePath);
        X = X. data;
    else
        nPatterns = 100;
        nPoints = 60;
        peakIndex = floor(nPoints * 0.40);
        peakHalfWidth = 3;
        allPeakIndices = peakIndex + (-peakHalfWidth:peakHalfWidth);
        nPeaks = 2 * peakHalfWidth + 1;
        noiseAmp = 0.5;
        X = randn(nPatterns, nPoints);
        for cntr = 1: nPatterns
            if (rand > 0.5)
                X(cntr,allPeakIndices) = noiseAmp * abs(randn(1, nPeaks));
            end
        end
    end
end
function corrCoeff = getCorrCoeff(a, b)
    sizeA = size(a);
    sizeB = size(b);
    if sizeA ~= sizeB
        corrCoeff = NaN;
    else
        a = a(:);
        b = b(:);
        a = a - mean(a);
        b = b - mean(b);
        corrCoeff = (a' * b) / (sqrt(a' * a) * sqrt(b' * b));
    end
end
function Initialize()
    close all force;
    clear;
    clc;
end    


