%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Exciter Script %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% This script calls 3 main functions, the Chebyshev Filter, a %%%%%%%
%%%%%%% harmoniser and a compressor. Written by M.Bechara, based on code by
%%%%%%% M.Holters. The Chebyshev Filter calls on an inbuilt Matlab function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %load Input signal
[x, fs] = audioread('Vocal.wav');

% High-Pass Filter inputs:
%Set the input arguments for the filter by defining the cutoff frequncy and
%the Nth order


fc = 5000;   %Cutoff in Hertz
wc=2*fc/fs;  %FIR inputs
N=40;         % Filter Order
type = 'high';
NC = N + 1; %Chebychev inputs
NO = N - 10;
% Harmoniser inputs:
%hBP is the bypass for the harmoniser set at 1 to bypass
hBP = 0;

maxlevel = -6; % Max level for normalisation

% Compressor inputs:
    gain = 7;      %Pregain 
    CT = -7;       %Threshold in dB
    CS = 1.6;      %Slope (Ratio)
    AT = 0.01;     %Attack Time
    RT = 0.003;    %Release Time
    
% Call the main function "Exciter"
y = Exciter(N, wc, type, NC, NO,hBP, x,fs, pgain, maxlevel, gain, CT, CS, AT, RT);

% Output of resulting sound will be "y"
% Playback, original then modified:
sound (x, fs)
pause (4)
sound (y, fs)
% audio write the output signal

audiowrite('ExcessiveGain.wav', y, fs);

% FFT function for plotting
Y1 = fft(x);
L1 = length(x);
PX2 = abs(Y1/L1);
PX1 = PX2(1:L1/2+1);
PX1(2:end-1) = 2*PX1(2:end-1);

Y2 = fft(y);
L2 = length(y);
PY2 = abs(Y2/L2);
PY1 = PY2(1:L2/2+1);
PY1(2:end-1) = 2*PY1(2:end-1);

fX = fs*(0:(L1/2))/L1;
fY = fs*(0:(L2/2))/L2;

figure(1)
plot(fX,PX1);
title('Input');
xlabel('Frequency (Hz)')
ylabel('Amplitude')
set (gca, 'Fontsize', 12)
figure(2)

plot(fY,PY1);
title('Output')
xlabel('Frequency')
ylabel('Amplitude')
set (gca, 'Fontsize', 12)
    