% Example_12_7
%
% Created for the book:
% Ljiljana D. Milic,
% Multirate Filtering for Digital Signal Processing: MATLAB Applications
% Information Science Reference, Hershey, New York, 2009
%
% Analysis and synthesis octave bank
clear all
close all
N = 18; fp = 0.4; % Seting the input parameters for firpr2chfb
[h0,h1,g0,g1] = firpr2chfb(N-1,fp); % Generating four filters for the orthogonal FIR filter bank
[H0,f] = freqz(h0,1,512,2);
[H1,f] = freqz(h1,1,512,2);
figure (1)
%plot(f,(abs(H0)),f,(abs(H1)),'b'), axis([0,1,0,1.1])
%hold on
%g(1)=axes('Position',[0.17 0.75 0.2 0.1]);
%g(1)=axes('Position',[0.24 0.70 0.25 0.1]);
%plot(f(1:205),(abs(H0(1:205)))), %axis([0,0.4,0.9998,1.00002])
%hold on
%g(2)=axes('Position',[0.62 0.70 0.25 0.1]);
%plot(f(307:512),(abs(H1(307:512)))), %axis([0.6,1,0.9998,1.00002])
%axis([0 1.1*fp/2 -2*Rp 2*Rp]), grid
%hold on
wname = 'db9'; % Set wavelet name.
% Compute the four filters associated with wavelet name given by the input string wname.
[h0,h1,g0,g1] = wfilters(wname);
N = 18; % The length of db9 filter
[H00,f]=freqz(h0,1,512,2);
[H11,f]=freqz(h1,1,512,2);
figure (1)
plot(f,(abs(H0)),'--',f,(abs(H1)),'--b',f,(abs(H00))/sqrt(2),'b',f,(abs(H11))/sqrt(2),'b'), grid
xlabel('Normalized frequency \omega/\pi'), ylabel('Magnitude')
axis([0,1,0,1.1])
hold on
g(1)=axes('Position',[0.20 0.65 0.25 0.1]);
plot(f(1:209),(abs(H00(1:209)))/sqrt(2),f(1:209),(abs(H0(1:209))),'--b'), axis([0,0.41,0.99,1.005])
g(2)=axes('Position',[0.62 0.65 0.25 0.1]);
plot(f(305:512),(abs(H11(305:512)))/sqrt(2),f(305:512),(abs(H1(305:512))),'--b'), axis([0.59,1,0.99,1.005])
%axis([0 1.1*fp/2 -2*Rp 2*Rp]), grid
% Compute the sum of the square magnitude responses
ver=abs(H0).^2+abs(H1).^2;
% Test signal
x=[zeros(size(1:20)),ones(size(21:50)),zeros(size(51:511))];
%x=[zeros(size(1:100)),0.01*(1:150),zeros(size(251:511))];
figure (2)
subplot(3,1,1)
plot(x)
title('Test signal')
xlabel('Time index n')
axis([0,512,-0.2,1.2])
% Analysis part
% Level 1
x0 = filter(h0,1,x);
x1 = filter(h1,1,x);
v0 = downsample(x0,2);
v1 = downsample(x1,2);
% Level 2
x2 = v0;
x02 = filter(h0,1,x2);
x12 = filter(h1,1,x2);
v02 = downsample(x02,2);
v12 = downsample(x12,2);
v2 = v12;
% Level 3
x3 = v02;
x03 = filter(h0,1,x3);
x13 = filter(h1,1,x3);
v03 = downsample(x03,2);
v13 = downsample(x13,2);
v3 = v13;
% Level 4
x4 = v03;
x04 = filter(h0,1,x4);
x14 = filter(h1,1,x4);
v04 = downsample(x04,2);
v14 = downsample(x14,2);
v4 = v14;
v5 = v04;
figure (3)
subplot(5,1,1)
plot(v1)
ylabel('v_1[n]')
axis([0,256,-0.5,0.5])
subplot(5,1,2)
plot(v2)
ylabel('v_2[n]')
axis([0,256,-0.5,0.5])
subplot(5,1,3)
plot(v3)
ylabel('v_3[n]')
axis([0,256,-0.5,1])
subplot(5,1,4)
plot(v4)
ylabel('v_4[n]')
axis([0,256,-0.5,0.5])
subplot(5,1,5)
plot(v5)
ylabel('v_5[n]')
xlabel('Time index n')
axis([0,256,-0.5,4])
% Synthesis part
% Level 4
w04=upsample(v5,2);
w14=upsample(v4,2);
y3=filter(g0,1,w04) + filter(g1,1,w14);
% Level 3
w13 = [zeros(size(1:N-1)),v3(1:length(v3)-(N-1))];
w13 = upsample(w13,2);
yu3 = upsample(y3,2);
y2 = filter(g0,1,yu3) + filter(g1,1,w13);
% Level 2
w12 = [zeros(size(1:3*(N-1))),v2(1:length(v2)-3*(N-1))];
w12 = upsample(w12,2);
yu2 = upsample(y2,2);
y1 = filter(g0,1,yu2) + filter(g1,1,w12);
% Level 1
w11 = [zeros(size(1:7*(N-1))),v1(1:length(v1)-7*(N-1))];
w11 = upsample(w11,2);
yu1 = upsample(y1,2);
y = filter(g0,1,yu1) + filter(g1,1,w11); % Reconstructed signal
figure (4)
%subplot(5,1,1)
%plot(v04)
%ylabel('v_5[n]')
%title ('Banka sinteze: rekonstrukcija signala')
%axis([0,64,-0.2,1.2])
subplot(4,1,1)
plot(y3)
ylabel('y_3(n)')
axis([0,512,-0.4,3.4])
subplot(4,1,2)
plot(y2)
ylabel('y_2[n]')
axis([0,512,-0.4,2.4])
subplot(4,1,3)
plot(y1)
axis([0,512,-0.2,1.2])
ylabel('y_1[n]')
axis([0,512,-0.4,2.4])
subplot(4,1,4)
plot(y)
ylabel('y[n]')
xlabel('Time index n')
axis([0,512,-0.2,1.2])
length(y)