view m-toolbox/test/MDC2/test_mdc2_noisegen_accuracy.m @ 35:4be5f7a5316f database-connection-manager

Suppress output messages on preferences loading and saving
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

% A test script for to check the accuracy of the noise generation procedure
% for the MDC2
% 
% 
% L. Ferraioli 04-02-2009
% 
% $Id: test_mdc2_noisegen_accuracy.m,v 1.4 2009/04/20 14:34:12 luigi Exp $
% 

%% General use variables and vectors

fs = 1;
f = logspace(-6,log10(fs/2),300);
f = f.';
Nsecs = 1e6; % number of seconds

%% MDC2 Models

b = ao(plist('built-in','mdc2r2_fd_ltpnoise','f',f));

%%

iplot(b)

%%

CSD = [b(1) b(2);conj(b(2)) b(3)];
MSC = b(2).*conj(b(2))./(b(1).*b(3));

%% some ploting

iplot(CSD(1,1),CSD(2,2),MSC)
iplot(CSD(1,1),CSD(2,2),CSD(1,2),CSD(2,1))

%% Make white noise

a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
a2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));

%% Noise generation

plng = plist(...
    'csd11', CSD(1,1), ...
    'csd12', CSD(1,2), ...
    'csd21', CSD(2,1), ...
    'csd22', CSD(2,2), ...
    'MaxIter', 80, ...
    'PoleType', 2, ...
    'MinOrder', 35, ...
    'MaxOrder', 40, ...
    'Weights', 2, ...
    'Plot', false,...
    'MSEVARTOL', 1e-2,...
    'FITTOL', 1e-5,...
    'UseSym', 0,...
    'Disp', false);

[ac1,ac2] = noisegen2D(a1,a2, plng);

% extracting filters
h11 = ac1.procinfo.find('Filt11');
h12 = ac1.procinfo.find('Filt12');
h21 = ac2.procinfo.find('Filt21');
h22 = ac2.procinfo.find('Filt22');


%% spectra calculation

acxx1 = lpsd(ac1);
acxx2 = lpsd(ac2);
acxx12 = lcohere(ac1,ac2);

% get dofs
dof1 = getdof(acxx1,plist('method','lpsd'));
dof2 = getdof(acxx2,plist('method','lpsd'));
dof12 = getdof(acxx12,plist('method','mslcohere'));

% Interpolate theorethical data
CSD11 = interp(CSD(1,1),plist('vertices',acxx1.data.x));
CSD22 = interp(CSD(2,2),plist('vertices',acxx2.data.x));
MSC = interp(MSC,plist('vertices',acxx12.data.x));

% getting variance
[lw1,up1,sig1] = confint(acxx1,plist('method','lpsd'));
[lw2,up2,sig2] = confint(acxx2,plist('method','lpsd'));
[lw12,up12,sig12] = confint(acxx12,plist('method','mslcohere'));

% chi square
k1 = acxx1 - CSD11;
k1 = k1.^2;
k1 = k1./sig1;
chi1 = sum(k1)./length(k1.data.x);

k2 = acxx2 - CSD22;
k2 = k2.^2;
k2 = k2./sig2;
chi2 = sum(k2)./length(k2.data.x);

k12 = acxx12 - MSC;
k12= k12.^2;
k12 = k12./sig12;
k12 = split(k12,plist('split_type','samples','samples',[3 342]));
chi12 = sum(k12)./length(k12.data.x);

proc1 = acxx1.procinfo;
proc2 = acxx2.procinfo;
proc12 = acxx12.procinfo;

%% 

iplot(CSD11,acxx1,lw1,up1)
iplot(CSD22,acxx2,lw2,up2)
iplot(MSC,acxx12,lw12,up12)

%% Running the loop

N = 100;

for ii = 2:N
  disp(num2str(ii))
  
  a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
  a2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));

  x11 = filter(a1,h11);
  x12 = filter(a2,h12);
  x21 = filter(a1,h21);
  x22 = filter(a2,h22);

  x1 = x11 + x12;
  x2 = x21 + x22;
  
  % sectra
  xx1 = lpsd(x1);
  xx2 = lpsd(x2); 
  xx12 = lcohere(x1,x2);
  
  % sum spectra
  acxx1 = acxx1 + xx1;
  acxx2 = acxx2 + xx2;
  acxx12 = acxx12 + xx12;
  
  % getting variance
  [lw,up,sig1] = confint(xx1,plist('method','lpsd'));
  [lw,up,sig2] = confint(xx2,plist('method','lpsd'));
  [lw,up,sig12] = confint(xx12,plist('method','mslcohere'));

  % chi square
  k1 = xx1 - CSD11;
  k1 = k1.^2;
  k1 = k1./sig1;
  xc1 = sum(k1)./length(k1.data.x);

  k2 = xx2 - CSD22;
  k2 = k2.^2;
  k2 = k2./sig2;
  xc2 = sum(k2)./length(k2.data.x);

  k12 = xx12 - MSC;
  k12= k12.^2;
  k12 = k12./sig12;
  k12 = split(k12,plist('split_type','samples','samples',[3 342]));
  xc12 = sum(k12)./length(k12.data.x);
  
  % adding chisquare
  chi1 = chi1 + xc1;
  chi2 = chi2 + xc2;
  chi12 = chi12 + xc12;
  
end

%% Do Averages

% do average
acxx1 = acxx1./N;
acxx1.setName;
acxx2 = acxx2./N;
acxx2.setName;
acxx12 = acxx12./N;
acxx12.setName;

% average chi square
chi1 = chi1./N;
chi2 = chi2./N;
chi12 = chi12./N;

% dofs for the averages
adof1 = dof1.*N;
adof2 = dof2.*N;
adof12 = dof12.*N;

% get confidence bounds for the 'true' spactra
[lw1,up1,sig1] = confint(acxx1,plist('method','lpsd','dof',adof1));
[lw2,up2,sig2] = confint(acxx2,plist('method','lpsd','dof',adof2));
[lw12,up12,sig12] = confint(acxx12,plist('method','mslcohere','dof',adof12));

%% plotting

iplot(CSD(1,1),acxx1)
iplot(CSD(2,2),acxx2)
iplot(MSC,acxx12)

iplot(CSD(1,1),lw1,up1)
iplot(CSD(2,2),lw2,up2)
iplot(MSC,lw12,up12)

%% plotting conflevels 1

x = lw1.data.x;
y1 = lw1.data.y;
y2 = up1.data.y;

figure
y = [y1 (y2-y1)]; % y1 and y2 are columns
ha = area(x, y);
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha(2), 'FaceColor', 'r')
set(ha, 'LineStyle', 'none')
grid on

% plot the line edges
hold on 
hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
hd = plot(CSD(1,1).data.x, CSD(1,1).data.y);

set(gca,'xscale','log','yscale','log');
% set(gca,'Layer','top')
ylabel('Power Spectrum [m^{2} / Hz]');
xlabel('Frequency [Hz]');
legend([hd hb],{'Model CSD(1,1)','95% Conf. level'});

%% plotting conflevels 2

x = lw2.data.x;
y1 = lw2.data.y;
y2 = up2.data.y;

figure
y = [y1 (y2-y1)]; % y1 and y2 are columns
ha = area(x, y);
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha(2), 'FaceColor', 'r')
set(ha, 'LineStyle', 'none')
grid on

% plot the line edges
hold on 
hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
hd = plot(CSD(2,2).data.x, CSD(2,2).data.y);

set(gca,'xscale','log','yscale','log');
% set(gca,'Layer','top')
ylabel('Power Spectrum [m^{2} / Hz]');
xlabel('Frequency [Hz]');
legend([hd hb],{'Model CSD(2,2)','95% Conf. level'});

%% plotting conflevels 3

x = lw12.data.x;
y1 = lw12.data.y;
y2 = up12.data.y;

figure
y = [y1 (y2-y1)]; % y1 and y2 are columns
ha = area(x, y);
set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
set(ha(2), 'FaceColor', 'r')
set(ha, 'LineStyle', 'none')
grid on

% plot the line edges
hold on 
hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
hd = plot(MSC.data.x, MSC.data.y);

set(gca,'xscale','log','yscale','log');
% set(gca,'Layer','top')
ylabel('Magnitude Square Coherence');
xlabel('Frequency [Hz]');
legend([hd hb],{'Model MSC','95% Conf. level'});