% Code accompanying fall 2025 mini project #2 to apply one level of the DWT
% Originally written by Dan Jacobellis 10/24/2025

% The coefficients below for a dyadic perfect reconstruction filterbank
% are based on the Cohen–Daubechies–Feauveau wavelet (bior6.8)
% If the wavelet toolbox is installed, you can also use the following code:
% [LA, HA, LS, HS] = wfilters('bior6.8');
% LA = LA(2:end)/sqrt(2); HA(2:end) = HA/sqrt(2);
% LS= sqrt(2)*LS(2:end); HS = sqrt(2)*HS(2:end);
coeffs = [
       0.00134974786501001                         0                         0      -0.00269949573002003
      -0.00135360470301001                         0                         0      -0.00270720940602003
       -0.0120141966670801        0.0102009221870399        0.0204018443740798        0.0240283933341602
       0.00843901203981008       -0.0102300708193699        0.0204601416387398        0.0168780240796202
        0.0351664733065404       -0.0556648607799594        -0.111329721559919       -0.0703329466130807
       -0.0546333136825205        0.0285444717151497       -0.0570889434302994        -0.109266627365041
       -0.0665099006248407         0.295463938592917         0.590927877185834         0.133019801249681
         0.297547906345713        -0.536628801791565          1.07325760358313         0.595095812691426
         0.584015752240756         0.295463938592917         0.590927877185834         -1.16803150448151
         0.297547906345713        0.0285444717151497       -0.0570889434302994         0.595095812691426
       -0.0665099006248407       -0.0556648607799594        -0.111329721559919         0.133019801249681
       -0.0546333136825205       -0.0102300708193699        0.0204601416387398        -0.109266627365041
        0.0351664733065404        0.0102009221870399        0.0204018443740798       -0.0703329466130807
       0.00843901203981008                         0                         0        0.0168780240796202
       -0.0120141966670801                         0                         0        0.0240283933341602
      -0.00135360470301001                         0                         0      -0.00270720940602003
       0.00134974786501001                         0                         0      -0.00269949573002003
];
LA = coeffs(:,1); % Lowpass Analysis
HA = coeffs(:,2); % Highpass Analysis
LS = coeffs(:,3); % Lowpass Synthesis
HS = coeffs(:,4); % Highpass Synthesis

% Example audio file built into matlab.
audiodata = load('handel.mat'); 
x = audiodata.y(1:end-1); fs = audiodata.Fs;

% Analysis
L1 = downsample(conv(x,LA),2);
H1 = downsample(conv(x,HA),2);

% Synthesis
xrec = conv(upsample(L1,2),LS) + conv(upsample(H1,2),HS);

% Account for delay
single_filter_delay = (length(coeffs)-1)/2;
num_cascaded_filters = 2;
total_delay = num_cascaded_filters*single_filter_delay;
xrec = xrec(total_delay:end-total_delay-1);

% verify that xrec is the same as x (accounting for small numerical error)
assert( max(abs(x-xrec)) < 1e-10 )

% create spectrograms for original signal and components
figure; spectrogram(x,1024,0,1024,'yaxis',fs);
clim([-70,0]); title('Original signal')

low_component = conv(upsample(L1,2),LS);
figure; spectrogram(low_component,1024,0,1024,'yaxis',fs);
clim([-70,0]); title('Low frequency component');

high_component = conv(upsample(H1,2),HS);
figure; spectrogram(high_component,1024,0,1024,'yaxis',fs);
clim([-70,0]); title('High frequency component');


