**********************
**********************
*LABORATORY SESSION 6*
**********************
**********************
%Auto-correlation
[x,fs] = wavread('aaa.wav');
[c,lags] = xcorr(x,x);
plot(lags,c)
%162 samples - fs/162 Hz

[x,fs] = wavread('iii.wav');
[c,lags] = xcorr(x,x);
plot(lags,c)
%140 samples - fs/140 Hz

[x,fs] = wavread('clarinet.wav');
[c,lags] = xcorr(x,x);
plot(lags,c)
%27 samples -> fs/27 = 296 Hz OR 27/8000 -> 0.0034s -> 1/ans = 296Hz -> nota D4

%Images
I = imread('0001.png'); % read an image
imshow(I); % show in image

figure; % plot images side by side
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(I);

%Image smoothing using following kernel 3x3 matrix with values 1/9
kernel = ones(3,3)/9;
J = imfilter(I,kernel);
imshow(J);

%Image smoothing using convolution and same kernel
C = conv2(double(I), kernel, 'same');
% A1: Rise and truncate pixels:
% convert values to unsigned 8 bit integer values which also performs truncating values below 0 and above 255 
Cs = unit8(C); 
% or
Cs = C + (2^8/2); Cs(Cs<0)=0; Cs(Cs>(2^8-1))=2^8-1; 
% Show image
imshow(Cs);

%Smoothing RGB image
RGB = imread('Lenna.png');
%We have do convolution in every dimension
Sr = conv2(double(RGB(:,:,1)), kernel, 'same');
Sg = conv2(double(RGB(:,:,2)), kernel, 'same');
Sb = conv2(double(RGB(:,:,3)), kernel, 'same');

fRGB = cat(3,uint8(Sr),uint8(Sg),uint8(Sb)); %concatenate all three dimensions
imshow(hRGB);

%Smoothing using gaussian filter
kernel=fspecial('gaussian', 3, 2); % Also check the others: sobel, laplacian, etc.
C = conv2(double(I), kernel, 'same');
% A2: Move and scale pixels:
Cm = C - min(C(:));
Cs = (2^8-1) .* (Cm ./ max(Cm(:)));
% Show image
imshow(Cs);

% Sharpening with Laplacian mask
kernel=ones(3,3); C(2,2)=-8;
C = conv2(double(I), kernel, 'same');
% A3: Rise, scale and truncate pixels:
Cs = (C + (2^8-1))./2;
% Show image
imshow(J);
