www.gusucode.com > wavelet 源码程序 matlab案例代码 > wavelet/DetectVarianceChangePointsExample.m

    %% Detect Variance Change Points  
% Add two variance change points to the blocks signal. Detect the variance
% change points using |wvarchg|.   

%% 
% Load the blocks signal. Add white noise with two variance change points
% located at index 180 and 600. 
x = wnoise(1,10);
rng default;
bb = 1.5*randn(1,length(x));
cp1 = 180; cp2 = 600;
x = x + [bb(1:cp1),bb(cp1+1:cp2)/4,bb(cp2+1:end)];  

%% 
% Obtain the level-1 wavelet coefficients. Replace the top 2% of values
% with the mean value of the wavelet coefficients to remove all signal. 
wname = 'db3'; lev = 1;
[c,l] = wavedec(x,lev,wname);
det = wrcoef('d',c,l,wname,1);
y = sort(abs(det));
v2p100 = y(fix(length(y)*0.98));
ind = find(abs(det)>v2p100);
det(ind) = mean(det);  

%% 
% Estimate the variance change points using the wavelet coefficients. 
[pts_Opt,kopt,t_est] = wvarchg(det,5);
sprintf('The estimated change points are %d and %d\n',pts_Opt)