% Attenuation signal 
alpha=300;      % alpha:attenuation constant
fc=200;    % fc:carrier frequency
% obserbvation time: 0 to 20ms
% Number of samples: N1=100samples
N1=100;
t1=linspace(0, 0.02, N1);
s1=exp(-alpha*t1).*sin(2*pi*fc*t1);
figure(1);
plot(t1,s1);grid on
xlabel('Time [sec]');
ylabel('Attenuation signal s1(t)');
% Non-linear time companding signal creation
dt=0.02/(N1-1);
for i=1:100
    t2(i)=t1(i)+0.002*sin(2*pi*dt*(i-1)/0.02);
end
s2=exp(-alpha*t2).*sin(2*pi*fc*t2);
figure(2);
plot(t1,s2);grid on
xlabel('Time [sec]');
ylabel('Attenuation signal s2(t)');

figure(3);
plot(t1,t2);grid on
xlabel('t1 [sec]');
ylabel('t2 [sec]');

% DP Matching
err(100,100)=zeros;
err_sum=0;
for i=1:100
    for j=1:100
        err(i,j)=abs(s1(i)-s2(j));
    end
end
s3(1:100)=zeros;
s3(1)=s2(1);
s3(100)=s2(100);
j=2;
for i=2:99
    if (err(i,j)<err(i-1,j)) && (err(i,j)<err(i,j-1))
        j=i;
    elseif (err(i-1,j)<err(i,j)) && (err(i-1,j)<err(i,j-1))
        j=i-1;
    else
        j=j-1;
    end
    s3(i)=s2(j);
    j=j+1;
end
    
figure(4);
plot(t1,s3);grid on
xlabel('Time [sec]');
ylabel('DP matchedattenuation signal s3(t)');
