%This is a script to generate transfer functions for fake amplifiers to
%demonstrate hard clipping, soft clipping, and asymmetric clipping on a
%sinusoid.

v = linspace(-1, 1);

Xsoft =[-1 -.99 -.98 -.97 .97 .98 .99 1];
Ysoft = [-.6 -.6 -.6 -.6 .6 .6 .6 .6];
Ysoft = interp1(Xsoft,Ysoft,v, 'spline');
%figure(1);
%plot(v, Ysoft);

Xhard = [-1 -.6 .6 1];
Yhard = [-.6 -.6 .6 .6];
Yhard = interp1(Xhard, Yhard, v);
%figure(2);
%plot(v, Yhard);

x = createsine(50, 2000, .02);
v2 = linspace(-1, 1, length(x));

% figure(3)
%plot(v2, x);

xs = interp1(v, Ysoft, x);
% figure(4)
%plot(v2, xs);
axis([-1 1 -1.5 1.5]);

xh = interp1(v, Yhard, x);
% figure(5)
%plot(v2, xh)
axis([-1 1 -1.5 1.5]);


figure(1)
plot(v, v);
hold on
plot(v, Ysoft, 'r');
hold on
plot(v, Yhard, 'g');

title('example amplifier transfer curves');
xlabel('input voltage');
ylabel('output voltage');

legend('perfect amp', 'vacuum tube - soft clipping', 'solid-state - hard clipping', 'Location', 'NorthWest')

figure(2)
plot(v2, x)
hold on
plot(v2, xs, 'r');
hold on
plot(v2, xh, 'g');
title('distorted sine wave')
xlabel('time');
ylabel('voltage');
legend('perfect amp', 'vacuum tube - soft clipping', 'solid-state - hard clipping', 'Location', 'NorthEast')

%asymmetric clipping
Xas =[-1 -.99 -.98 -.97 0 .97 .98 .99 1];
Yas = [-.4 -.4 -.4 -.4 0 .7 .7 .7 .7];
Yas = interp1(Xas,Yas,v, 'spline');
figure(3)
plot(v, v);
hold on
plot(v, Yas, 'r');
title('asymmetric transfer function');
xlabel('input voltage');
ylabel('output voltage');
legend('perfect amp', 'vacuum tube', 'Location', 'NorthWest')

xa = interp1(v, Yas, x);
figure(4)
plot(v2, x)
hold on
plot(v2, xa, 'r');
title('distorted sine wave - assymetric');
xlabel('time')
ylabel('voltage')
legend('perfect amp', 'vacuum tube', 'Location', 'NorthEast')
