%PWL
%===============================================================================
% Title: PWL.m
%
% Project: 1D signal:Identification of PieceWise Linear by multiple regression
% Detection of homogeneous zone using entropie
% Projection in the Hough space (1D)
% I/O
% Inputs: 1D signal (xe,ye)
% Simulated data (Figure 12);
% Outputs: Entropie signal (xee,yee)
% Line positions, position
% Line lengths, longueur
% Line slopes, pente
% Error of regression, e
%
% Purpose: E=0:
% Identification of PieceWise Linear (Figure 1)
% Projection in the Hough space (Figure 2,3)
% E=1:
% Identify homogeneous zone of a signal (detect also singularity or abrupt changes)(Figure 1)
% Projection in the 1D Hough space (Length of the line,position of the line) (Figure 2,3)
%
% Codes also used corrcoef, polyfit, plotc, plumbplot3
%
%
% Author: MORLIER Joseph
%
% History: date subject
%
% 27.10.04 Initial Version
%
%
% Contact: jmorlier@lrbb.u-bordeaux.fr
%
%
%
%===============================================================================
clear all;close all;
E=0;% PWL only : line detection
%E=1;% Entropie : find homogeneous zone
%%%%%%%%%%%%%%%%%%%%%%%%%%
%SIMULATED DATA
x1=0:0.1:2;
y1=(2*x1+1);
x2=2.1:0.1:4;
y2=(4*x2+2);
x3=4.1:0.1:6;
y2=(-7*x3+0.5);
xee=[x1 x2 x3];
yee=[y1 y2 x3] ;yee=yee+0.1*rand(1,length(yee));
figure(12);plot(xee,yee);title('Initial data');
if E==1
[xee,yee]=entropie(xee,yee);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%INITIAL VALUE
u=0;R=zeros(1,20);
error=1;err=[];
erreur=[];pente=[];longueur=[];position=[];
figure(1);plot(xee,yee);
if E==1
title('Signal Entropie');
else
title('PieceWise Linear signal');
end
hold on;
B=round(length(yee));
A=1;
var=['g' 'k' 'y' 'b' 'r' 'g' 'k' 'y' 'b' 'r'];var=[var var var];
pos=0;
if pos<length(yee)-2
%%%%%%%%%%%%%%%%%%%%%%%%%%
%LOOP MULTIPLE REGRESSION
%STOPING CRITERION R>0.99
while u<20
u=u+1;
if u==1
j=B;A=A;
else
B=length(yee)-1;
A=j;
j=B;
error=1;
end
while R(u)<0.99 & j>A+1
j=j-1;
yei=yee(A:j);
xei=xee(A:j);
%%%%%%%%%%%%%%%%%%%%%%%%
%Slope of the PWL
alpha=polyfit(xei,yei,1);
%%%%%%%%%%%%%%%%%%%%%%%%
%Linear regression
%R estimation
r2=corrcoef(xei,yei);
R(u)=r2(1,2)^2;
%%%%%%%%%%%%%%%%%%%%%%%%
%Calculus of the detected line
yep=(xei*alpha(1))+alpha(2);
%%%%%%%%%%%%%%%%%%%%%%%%
%Error between detected line
%and simulated data
erreur(j)=mean(abs(yei-yep));
err(u,j)=erreur(j);
error=erreur(j);
e(u)=error;
%%%%%%%%%%%%%%%%%%%%%%%%
% Hough space parameters
pente(u)=(alpha(1)*180)/pi;
longueur(u)=length(yei);
position(u)=j;
pos=position(u);
end
%%%%%%%%%%%%%%%%%%%%%%%
%Display of the detected line
figure(1);hold on;
plot(xei,yep,var(u));hold on;
end
end
figure(2);
plotc(xee(position),longueur,e);grid on;
xlabel('line position');ylabel('line length');zlabel(' regression error'); Title('Hough space 2');
figure(3);
plumbplot3(xee(position),pente,longueur);grid on;
xlabel('line position');ylabel('line slope');zlabel('length');Title('Hough space 3');