clc, clear all, close all, clc;% commands which delete all the variables,
% close all the opened figures and clear the workspace
%% Task 1.4.
%% Algorithm:
% 1. Generate the sampling frequency Fs
% 2. Make a vector x, which length is 8192 samples
% 3. Generate frequencies f1, f2, f3 with 3 different values
% 4. Create 3 signals y1, y2, y3 with corresponding frequencies f1, f2, f3
% 5. Listen and plot these three signals using the function (sound)
% 6. Listen and plot the sum of the first 2 signals
% 7. Listen and plot the sum of all signals
%% Solution:
Fs = 8192; % sampling frequency Fs, [Hz]
x = 0:(Fs-1); % vector x (8192 samples)
f1 = 200; % 1st frequency
f2 = 400; % 2nd frequency
f3 = 800; % 3rd frequency
y1 = cos(2*pi*x*f1/Fs); % 1st sihnal
y2 = cos(2*pi*x*f2/Fs); % 2nd signal
y3 = cos(2*pi*x*f3/Fs); % 3rd signal
sound(y1, Fs); % listen to the 1st signal
sound(y2, Fs); % listen to the 2nd signal
sound(y3, Fs); % listen to the 3rd signal
sound(y1+y2, Fs); % listen to the sum of 1st and 2nd signal
sound(y1+y2+y3, Fs); % listen to the sum of all signals
%% Task 1.5.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the file handel to the same folder where your script is located.
% Note that you do not need to write '.mat' while loading file!
% 3. Generate a vector N, which length is 2 times higher than sampling
% frequency Fs
% 4. short the signal y as it sais in the task
% 5. Listen to the signal y by using command soundsc
% 6. generate noise (you can call this variable 'noise') by using comand
% randn
% 7. Define the variable k, which is used to change the amplitude of the
% noise
% 8. Generate the signal 'z', which is a sum of the signals 'y' and
% 'noise'
% 9. Listen to the signal 'z'
%% Solution:
clc, clear all, close all;
load handel;
N = 2*Fs;
y = y(1:N);
soundsc(y, Fs);
noise = randn(N,1);
k = 0.1;
z = y + k*noise;
soundsc(z, Fs);
%% Task 1.6.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Generate two vectors 'x' and 'y'
% 3. Generate figure (1), which consists of 2 graps. Use 'stem' function to
% generate sequencies 'x' and 'y'. Give a title and names for axis.
% 4. Generate a new figure (2) and plot there the sum of x and y
% 5. Generate a new figure (3) and plot there the multiplication of x and y
% 6. Generate a new figure (4) and plot there the convolution of x and y
%% Solution:
clc, clear all, close all;
x = [1 0 2 3];
y = [0 0 3 2];
figure(1);
subplot(211), stem(x), title('Vector x'), xlabel('sequence'), ylabel('amplitude');
subplot(212), stem(y), title('Vector y'), xlabel('sequence'), ylabel('amplitude');
figure(2);
stem(x+y), title('x + y');
figure(3);
stem(x.*y), title('x.*y');
figure(4);
stem(conv(x,y)), title('x (*) y');
%% Task 1.7.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Create a sampling frequency Fs
% 3. Make a vector x, which length is 8192 samples
% 4. Generate frequencies f1, f2, f3 with 3 different values
% 5. Create 3 signals y1, y2, y3 with corresponding frequencies f1, f2, f3
% 6. Generate the signal 'h1', which moves the signal to the right by 8192
% samples (=1 second delay) and multiplies it with 0.5 when convoluted
% with signal
% 7. Generate the signal 'h2', with a 3 second delay and which multiplies
% it with 0.1 when convoluted with signal
% 8. Generate the signal z1, which is a convolution of h1 and the sum of
% signals
% 9. Generate the signal z2. Convolute the sum of signals with h2
%% Solution:
clc, clear all, close all;
Fs = 8192;
x = 0:(Fs-1);
f1 = 200;
f2 = 400;
f3 = 800;
y1 = cos(2*pi*x*f1/Fs);
y2 = cos(2*pi*x*f2/Fs);
y3 = cos(2*pi*x*f3/Fs);
h1 = [zeros(1,Fs) 0.5];
h2 = [zeros(1,Fs*3) 0.1];
z1 = conv(h1, (y1+y2+y3));
z2 = conv(h2, (y1+y2+y3));
sound(z1, Fs);
sound(z2, Fs);
%% Task 1.8.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the file toomuch
% 3. Generate the signal y, which is equal to toomuch
% 4. Generate a vector h, which creates the echo-effect when convoluted
% with the signal. Every echo comes 0.3 second later and with half the
% loudness as the one before
% 5. Create a signal z, which is a convolution of h and y
%% Solution:
clc, clear all, close all;
load toomuch
y = toomuch;
h = [1 zeros(1,fs*0.3) 0.5 zeros(1,fs*0.3) 0.125 zeros(1,fs*0.3) 0.0156];
z = conv(h,y);
sound(z, fs);
%% Task 1.9.
%% Algorithm:
% 1. File => New => Function
% 2. Instead of [ output_args ] write x
% 3. Instead of input_args put the 'seq', 'N' and 'delay'
% 4. Give a name for the outer function instead of Untitled1. For instance,
% unitsig
% 5. Write the body of the outer function
% 6. Save the outer function in the same folder where is you script. DO NOT
% simulate it!
% 7. In your script file use the outer function to generate the unistep
% function.
% 8. Plot it.
%% Solution:
% function [ x ] = untsig(seq, N, delay)
% %UNTITLED2 Summary of this function goes here
% % Detailed explanation goes here
% if (seq(1)=='d')
% x = [zeros(delay,1); 1; zeros(N-delay-1,1)];
% end
%% ======================================================================%%
% LAB 2 %
%%=======================================================================%%
%% Task 2.6.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Generate the sampling frequency Fs
% 3. Make a vector n, which length is 8192 samples
% 4. generate 8 different frequencies f1, f2...f8 from 1000 till 8000 Hz
% 5. Generate 8 signals x1, x2....x8 with these frequencies
% 6. Listen to the signals
%% Solution:
clc, clear all, close all;
Fs=8192; % sampling frequency
n=[1:1:8192]; % samplings
f1=1000;
f2=2000;
f3=3000;
f4=4000;
f5=5000;
f6=6000;
f7=7000;
f8=8000;
x1=cos(2*pi*n*f1/Fs);
x2=cos(2*pi*n*f2/Fs);
x3=cos(2*pi*n*f3/Fs);
x4=cos(2*pi*n*f4/Fs);
x5=cos(2*pi*n*f5/Fs);
x6=cos(2*pi*n*f6/Fs);
x7=cos(2*pi*n*f7/Fs);
x8=cos(2*pi*n*f8/Fs);
sound(x1,Fs);
sound(x2,Fs);
sound(x3,Fs);
sound(x4,Fs);
sound(x5,Fs);
sound(x6,Fs);
sound(x7,Fs);
sound(x8,Fs);
%% Task 2.7.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file bird1
% 3. Generate sampling frequency Fs
% 4. Listen to the signal (use the variable, which is at the Workspace)
% 5. Look at the spectrogram
% 6. Resample the signal by picking every second sample from it. HINT: you
% can just create a new vector y1, which is the same as the x vector
% from the m-file bird1, but take every second sample.
% 7. Create another vector y2, but use the function 'decimate'
% 8. Listen to this signals
% 9. Plot the spectrogram of these signals
%% Solution:
clc, clear all, close all;
load bird1;
Fs=200;
sound(x,Fs);
SPECTROGRAM(x);
y1 = x(1:2:end);
y2 = decimate(x,2);
sound(y1, Fs/2);
figure;
specgram(y1);
sound(y2, Fs/2);
figure;
specgram(y2);
%% Task 2.8.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file toomuch
% 3. Listen to the signal
% 4. Generate a spectrogram of the signal
% 5. Create a vector B
% 6. Convolute the signal with the vector B
% 7. Draw the picture
% 8. Listen to the convoluted signal
%% Solution:
clc, clear all, close all;
load toomuch
sound(toomuch, fs);
figure(1);
specgram(toomuch);
b = [-0.0039 0 0.0321 0.1167 0.2207 0.2687 0.2207 0.1167 0.0321 0 -0.0039];
figure(3);
z = conv(toomuch, b);
specgram(z);
sound(z, fs)
%% ======================================================================%%
% LAB 3 %
%%=======================================================================%%
%% Task 3.4.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file toomuch
% 3. Cut the first part 'too' which lasts ... samples
% 4. Get the part 't' wich is the first ... samples
% 5. Get the letter 'h' wich lasts from the ... samples till the end
% 6. Generate the signal tooth. It is a new vector, which consists of vectors
% 'too', 't' and 'h'
% 7. Listen to the signal. Use the function 'soundsc'
%% Solution:
clc, clear all, close all;
load toomuch
too = toomuch(1:1550);
t = toomuch(1:550);
h = toomuch(5700:end);
tooth = [too' t' h']';
soundsc(tooth, fs);
%% Task 3.5.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file 'toomuch'
% 3. Normalize the signal so you use the whole range (max, abs)
% 4. Make a linear quantization q1 and q2 (two levels) of the toomuch
% signal
% 5. Listen to the normilized toomuch
% 6. Listen and plot the quantized signal q1
% 7. Listen and plot to the quantized signal q2
%% Solution:
clc, clear all, close all;
load toomuch
toomuch = toomuch ./ max(abs(toomuch));
q1 = quant(toomuch, 2^(0));
q2 = quant(toomuch, 2^(-1));
soundsc(toomuch, fs);
soundsc(q1, fs);
soundsc(q2, fs);
%% Task 3.6.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file 'toomuch'
% 3. Normalize the signal so you use the whole range (max, abs)
% COMPRESSION
% 4. Determine mu
% 5. Determine the mu-law
% QUANTIZATION
% 6. Make two quant levels q1 and q2
% EXPANDING
% 7. Determine expanding signals x1 and x2
% 8. Listen to the signal toomuch, x1 and x2
% 9. Plot 4 signals (original, companded signal,quantized with 1 bit signal,expanded signal) at 1 figure
%% Solution:
clc, clear all, close all;
load toomuch
toomuch = toomuch ./ max(abs(toomuch));
% COMPRESSION
mu = 255;
y = sign(toomuch).*((log(1 + mu*abs(toomuch)))/(log(1+mu)));
% QUANTIZATION
q1 = quant(y, 2^(0));
q2 = quant(y, 2^(-1));
% EXPANDING
x1 = sign(q1).*(((1+mu).^(abs(q1))-1)/mu);
x2 = sign(q2).*(((1+mu).^(abs(q2))-1)/mu);
soundsc(toomuch, fs);
soundsc(x1, fs);
soundsc(x2, fs);
%% Task 3.7.
%% Algorithm:
% 1. Close all Figure windows and clear the workspace
% 2. Load the m-file 'test'
% 3. Listen to the signal from the test.mat
% 4. Generate a spectrogram of the signal
% 5. Create 'fB', which is a divided band of the signal 'y'
% 6. Define the energy of the each band
% 7. Define different bands 'band1',...'band4' with values which have the most
% energy
% 8. Create a new signal (vector) 'fBq' which consists of zeros and which size is
% the size of 'fB'
% 9. Quantize the signal 'fBq' with two levels
% 10. Quantize the signal 'fBq' for the band1,...band4 with a chosen number
% of bits (define yourself)
% 11. Reconstruct the signal by using the outer function CombineBands
% 12. Listen to this reconstructed signal
% 13. Draw the specrogram of this reconstructed signal
%% Solution:
clc, clear all, close all;
load test
soundsc(y)
specgram(y);
fB = DivideBands(y);
% energy content of each band, 4 bands seem to have to most energy of
% the signal
sum(fB.^2, 2)
% the interesting bands are
band1 = 2;
band2 = 4;
band3 = 7;
band4 = 16;
% so we allocate most bits (8*256*4) to those bands,
% and only one bit / sample for rest (1*256*28)
% resulting 15360bit/second (the test sample is one second long)
fBq = zeros(size(fB));
fBq = quant(fB, 2^(-1+1));
fBq(band1, :) = quant(fB(band1, :), 2^(-8+1));
fBq(band2, :) = quant(fB(band2, :), 2^(-8+1));
fBq(band3, :) = quant(fB(band3, :), 2^(-8+1));
fBq(band4, :) = quant(fB(band4, :), 2^(-8+1));
x = CombineBands(fBq);
% now we have in x the reconstructed signal
soundsc(x)
figure; specgram(x)