Project Development: March 2010

## Sunday, March 21, 2010

### Speech Processing, Proj02

//Purdue Cal - ECE 595C.
//Spring 2010.
//Project 02.
//Copyright @ 2010 antonio081014  antonio081014 ;
//All codes are in Matlab.

Task 1: Obtain estimated F0  by using the log harmonic product spectrum with K = 4 and K=10.

PS: The log product of the harmonic spectrum is the summation of the log harmonic spectrum.\
1. Down sampling with K=4 and K=10 to get the spectrum for each case.
2. Sum them up.
3. Find the highest peak in the final spectrum. That is the estimated F0.

Code:
fdate = fft(dat(:,15).*hamming(320), 1024);  % The 15th frame in the data.
tmp = log(abs(fdat3(1:512)));
figure;
subplot(4,1,1);
plot(tmp);
title(['The frequency plot of the frame #:', num2str(23)]);
xlabel('Freq index');
ylabel('Log-Magnitude');
hold on;
tmp1 = downsample(tmp, 4);
z = zeros(512 - length(tmp1),1);
tmp1 = [tmp1; z];
tmp2 = downsample(tmp, 10);
z = zeros(512 - length(tmp2),1);
tmp2 = [tmp2; z];

subplot(4,1,2);
plot(tmp1, 'r');
xlabel('Freq index, K=4');
ylabel('Log-Magnitude');

subplot(4,1,3);
plot(tmp2, 'g');
xlabel('Freq index, K=10');
ylabel('Log-Magnitude');

product = tmp + tmp1 + tmp2;
subplot(4,1,4);
plot(product, 'r');
xlabel('Freq index');
ylabel('Log-Magnitude');
[x, y] = ginput(3);
% x = [11.79; 57.60; 93.18;]; These are the index in frequency domain.
Task 2: Using overlap-add method, reconstruct the given utterance after filtering each frame using a bandpass filter from 50Hz-2000Hz.

1. Filter each frame.
2. Reconstruct the utterance by using overlap-add method.

Code:
% Filter:
recdat = zeros(320, 100);
for i=1:100                                    % There is 100 frames here.
fmdate = dat(:,i);
ffmdat = fft(fmdate.*hamming(320),1024); % Take Fourier Transform using 1024 points.
LF = ceil(1024 / fs * 50);
HF = floor(1024 / fs * 2000);
ffmdat(1:LF,1) = 0;
ffmdat(HF:512,1) = 0;
ffmdat(512:end-HF,1) = 0;
ffmdat(end-LF:end,1) = 0;
recdat(:,i) = real(ifft(ffmdat,320));
clear ffmdat;
end
% Reconstruct
update_sp = zeros(1,16000);
start = 1;
stop = start + 320 - 1;
for i=1:100
update_sp(start:stop) = update_sp(start:stop) + recdat(:,i)';    % overlap-add;
start = start + 160;
stop = start + 320 - 1;
if stop > 16000
break
end
end

figure;
plot(update_sp);
title('The filterd speech.');
% soundsc(update_sp);

fm = recdat(:,23);
ffm = fft(fm.*hamming(320), 1024);
figure;
plot(abs(ffm(1:512)));
title(['The filterd frame #:', num2str(23)]);
[xx, yy] = ginput(3);
% xx = 35.2534562211982;72.5806451612903;107.142857142857;];

%%
figure;
x = nn_spch;
window = 128;
noverlap = 64;
nfft = 128;
fs = 16000;
spectrogram(x,window,noverlap,nfft,fs, 'yaxis');
colormap(gray);
title('Power Spectral Density, Original');
xlabel('In Time Domain.');
ylabel('In Freq Domain.');

figure;
x = update_sp;
window = 128;
noverlap = 64;
nfft = 128;
fs = 16000;
spectrogram(x,window,noverlap,nfft,fs, 'yaxis');
colormap(gray);
title('Power Spectral Density, Filterd');
xlabel('In Time Domain.');
ylabel('In Freq Domain.');

### Speech Processing, Proj03

//Purdue Cal - ECE 595C.
//Spring 2010.
//Project 03.

//All codes are in Matlab.

Task One: How to plot the frequency spectrum and the spectrum envelop.
1. use the Matlab function lpc to generalize the coefficients alphas for the windowed samples in each frame.
2. use the Matlab function freqz to plot the spectrum envelop.
3. use the Matlab fft to generalize the features in Frequency domain, and then plot it in half (symmetry).
Code:

for i=1:3
ftmp = fft(data(:,i), 2048);   % data is the windowed signal.
[a, g] = lpc(data(:,i), 7);       % 7th order.
figure;
hold on;
[h, w] = freqz([1], a, 1024, fs);  % plot the spectrum envelop.
plot(abs(h) / max(abs(h)), 'r');
plot(abs(ftmp(1:1024)) / max(abs(ftmp(1:1024))), 'g'); % plot the freq spectrum.
title(['Frame: #', num2str(i)]);
legend('The spectrum envelop','The freq spectrum');
end

It shows the spectrum envelop in red, and the frequency feature in green.

Task Two: Generate the LP model by using Toeplitz Method.
1. Get the autocorrelation of the original signals.
2. Generate the matrix by using Matlab function Toeplitz .
3. Get the coefficients alphas according to the normal equations in the matrix form.

Code:
tmp_data = norm_spch(201:600);
c = xcorr(tmp_data,4, 'coeff');    % It can generate 2 * N -1 poles there. 4 -> 7.
T = toeplitz(c(2:end));  % The first is 1, which is not necessary.
alpha = inv(T) * c(2:end);
lpca = lpc(tmp_data, 7);
% Verified through the following steps.
test_sp = filter([0  -lpca(2:end)], 1, tmp_data);
plot(test_sp, 'r');
hold on
plot(tmp_data, 'g');
err_sig = tmp_data - test_sp;
figure;
plot(err_sig);
title('The error signal generated by using Toeplitz Method');
xlabel('Time');
ylabel('Magnitude');

It plots the original signal (green) and the estimated signal (red) generated by using Toeplitz method.