

[input,fs ] = wavread('lead_guitar.wav'); %load in the audio file to be 
%processed.

%--------------------------------------------------------------------------
%audio sample file names to copy and paste into 'wavread'
%--------------------------------------------------------------------------
%  'lead_guitar.wav' % audio file name (sample)
%  'sine_wave.wav'   % audio file name (sample)
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%First filter; high-pass to reduce unwanted subharmonic frequencies. Using
%the 'fir1' MATLAB function, this filter is linear phase, Hamming window
%based response. Using the 'ftype' argument 'high' or 'low', yields either
%a high-pass or low-pass filter response. Clipping a signal with
%reduced low frequency content yeilds a clearer, more defined tone through
%higher levels of clipping. Varying the cutoff frequency and resolution of
%the filter changes the way the signal is clipped and also changes the tone
%of the output signal. Minimal reduction in low frequency may produce a
%muddy, boomy or 'thick' tone. higher reduction may produce a glassy,
%peircing tone. Which tone is 'best' is totally subjective; it is up to the
%user and the purpose of the audio that determines this attribute.
%--------------------------------------------------------------------------
cut_off_frequency = 200; %everything BELOW the frequency specified here
%will be attenuated by the filter
normalized_cut_off_frequency = cut_off_frequency/(fs/2); 
%create a normalized value between 0 and 1. Given by dividing the specified 
%cutoff frequency by the Nyquist frequency. This is required so that the 
%magnitude response of the passband is 0dB.
coefficient_number = 128; %set the number of poles acting on the signal by
%filter. Higher values yeild a higher resolution response wth a steeper
%transition to the stopband.
hi_pass_1 = fir1(coefficient_number, normalized_cut_off_frequency, 'high');
%set the variables for the filter
%--------------------------------------------------------------------------
hp1 = filter(hi_pass_1, 1, input);
%filter the input signal with the filter variables specified and create the
%output signal (hp1)
%--------------------------------------------------------------------------

%freqz(hi_pass_1, 1, 1024, fs)

%--------------------------------------------------------------------------
%First gain stage. This stage sets the variables and calls the
%'eight_power' function. It is receiving the output signal from the first
%high-pass filter (hp1). The definition and function of the variables gain,
%bais, and mix apply to the following four gain stages
%--------------------------------------------------------------------------
gain = 1; %Inceases the amplitude of the signal. Higher values will be 
%clipped in an increasingly extreme manner. It also shifts the balance of
%odd-order and even-order harmonics. Lower values yeild more even order,
%higher values (increasingly clipped), will yeild more odd. 
bias = -0.01; %Simulates the non-linear transfer between +ve and -ve current
% when a valve reaches saturation.
mix = 0.5;% blend the original signal with the clipped signal. 1 is 100% 
%wet, distorted signal. The blend between wet and dry also changes the
%harmonic content of the output signal and therefore its sonic character.
valve_1 = eight_power(hp1, gain, bias, mix); %set the variables, call the
%function and create the output signal (valve_1).
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%First low-pass filter. Low-pass to reduce unwanted higher harmonics. Using
%the 'fir1' MATLAB function, this filter is linear phase, Hamming window
%based response. 
%--------------------------------------------------------------------------
cut_off_frequency = 1000;%everything ABOVE the frequency specified here
%will be attenuated by the filter
normalized_cut_off_frequency = cut_off_frequency/(fs/2);%create a
%normalized value between 0 and 1. Given by dividing the specified cutoff
%frequency by the Nyquist frequency. 
coefficient_number = 32;%set the number of poles acting on the signal by
%filter. Higher values yeild a higher resolution response wth a steeper
%transition to the stopband.
lo_pass_1 = fir1(coefficient_number, normalized_cut_off_frequency, 'low');
%set the variables for the filter
%--------------------------------------------------------------------------
lp1 = filter(lo_pass_1, 1, valve_1);
%filter the input signal with the filter variables specified and create the
%output signal (lp1)
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Second gain stage. This stage sets the variables and calls the
%'four_power' function. It is receiving the output signal from the first
%low-pass filter (hp1). 
%--------------------------------------------------------------------------
gain = 1;
bias = -0.1;
mix = 0.5;
valve_2 = four_power(lp1, gain, bias, mix);
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Second high-pass filter. Using the same variables as the first high-pass
%filter however, different values are used here. 
%--------------------------------------------------------------------------
cut_off_frequency = 600;
normalized_cut_off_frequency = cut_off_frequency/(fs/2);
coefficient_number = 256;
hi_pass_2 = fir1(coefficient_number, normalized_cut_off_frequency, 'high');
%--------------------------------------------------------------------------
hp2 = filter(hi_pass_2, 1, valve_2); %Apply filter, output signal.
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Third gain stage. This stage sets the variables and calls the
%'four_power' function. It is receiving the output signal from the second
%high-pass filter (hp2).
%--------------------------------------------------------------------------
gain = 1;
bias = -0.01;
mix = 0.5;
valve_3 = four_power(hp2, gain, bias, mix);
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Second low-pass filter. Using the same variables as the first low-pass
%filter however, different values are used here.
%--------------------------------------------------------------------------
cut_off_frequency = 4000;
normalized_cut_off_frequency = cut_off_frequency/(fs/2);
coefficient_number = 32;
lo_pass_2 = fir1(coefficient_number, normalized_cut_off_frequency, 'low');
%--------------------------------------------------------------------------
lp2 = filter(lo_pass_2, 1, valve_3); %Apply filter, output signal.
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
gain = 1;
bias = -0.01;
mix = 0.5;
valve_4 = two_power(lp2, gain, bias, mix);
%--------------------------------------------------------------------------
%Fourth gain stage. This stage sets the variables and calls the
%'two_power' function. It is receiving the output signal from the second
%low-pass filter (lp2).
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
cut_off_frequency = 4500;
normalized_cut_off_frequency = cut_off_frequency/(fs/2);
coefficient_number = 32;
lo_pass_3 = fir1(coefficient_number, normalized_cut_off_frequency, 'low');
%--------------------------------------------------------------------------
%Third low-pass filter. Using the same variables as the first low-pass
%filter however, different values are used here.
%--------------------------------------------------------------------------
lp3 = filter(lo_pass_3, 1, valve_4); %Apply filter, output signal.
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Mix control. For further tonal options, blend the original signal back
%in with the processed signal.
%--------------------------------------------------------------------------
dw_mix = 1;
global_mix = dw_mix*lp3 +(1-dw_mix)*hp1;
%--------------------------------------------------------------------------


%--------------------------------------------------------------------------
%Playback the processed sound file.
%--------------------------------------------------------------------------
sound(global_mix,fs);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------






