0% found this document useful (0 votes)
7 views15 pages

Speech Signal Processing Techniques

The document details the process of speech signal acquisition, including reading, filtering, decimating, and reconstructing speech signals using MATLAB code. It includes visualizations of the original, compressed, and reconstructed speech signals, as well as their frequency spectra and energy comparisons. Additionally, it discusses the error signal between the original and reconstructed signals and provides audio playback of the various stages.

Uploaded by

tehmassikandar00
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Speech Signal Processing Techniques

The document details the process of speech signal acquisition, including reading, filtering, decimating, and reconstructing speech signals using MATLAB code. It includes visualizations of the original, compressed, and reconstructed speech signals, as well as their frequency spectra and energy comparisons. Additionally, it discusses the error signal between the original and reconstructed signals and provides audio playback of the various stages.

Uploaded by

tehmassikandar00
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Speech Signal Acquisition

Code:
clc; clear; close all;
% Read speech signal
[x, Fs] = audioread('[Link]');
% Convert to mono if stereo
x = x(:,1);
% Time axis
t = (0:length(x)-1)/Fs;
% Plot
figure;
plot(t, x);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Original Speech Signal');
grid on;

plots:

Original Speech Signal


1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)
Frequency Spectrum of Original Speech
Code:
clc; clear; close all;

[x, Fs] = audioread('[Link]');


x = x(:,1);

N = 2048;
X = abs(fft(x, N));
f = linspace(0, Fs/2, N/2);

figure;
plot(f, X(1:N/2));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Frequency Spectrum of Original Speech');
grid on;
plots:
Frequency Spectrum of Original Speech
1

0.8

0.6

0.4

0.2
Magnitude

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5
Frequency (Hz) 4
10
Anti-Aliasing FIR Low-Pass Filter Design
Code:
clc; clear; close all;

[x, Fs] = audioread('[Link]');

M = 2; % Decimation factor
Fc = Fs/(2*M); % Cutoff frequency
Nf = 80; % Filter order
Wn = Fc/(Fs/2); % Normalized cutoff

h = fir1(Nf, Wn); % FIR LPF

figure;
freqz(h, 1, 1024, Fs);
title('Anti-Aliasing FIR Low Pass Filter');

plots:

Anti-Aliasing FIR Low Pass Filter


50
Magnitude (dB)

-50

-100

-150
0 0.5 1 1.5 2
Frequency (Hz) 4
10
0
Phase (degrees)

-2000

-4000

-6000
0 0.5 1 1.5 2
Frequency (Hz) 4
10
Speech Compression Using Decimation
Code:
clc; clear; close all;
[x, Fs] = audioread('[Link]');
x = x(:,1);
M = 2;
Fc = Fs/(2*M);
Nf = 80;
Wn = Fc/(Fs/2);
h = fir1(Nf, Wn);
% Anti-aliasing filter
x_filt = filter(h, 1, x);
% Decimation
x_dec = downsample(x_filt, M);
Fs_dec = Fs / M;
t_dec = (0:length(x_dec)-1)/Fs_dec;
figure;
plot(t_dec, x_dec);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Compressed Speech Signal (Decimated)');
grid on;
plot:
Compressed Speech Signal (Decimated)
1.5

0.5
Amplitude

-0.5

-1
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)
Spectrum of Compressed Speech
Code:
clc; clear; close all;
[x, Fs] = audioread('[Link]');
x = x(:,1);
M = 2;
h = fir1(80, (Fs/(2*M))/(Fs/2));
x_dec = downsample(filter(h,1,x), M);
Fs_dec = Fs/M;
N = 2048;
X_dec = abs(fft(x_dec, N));
f_dec = linspace(0, Fs_dec/2, N/2);
figure;
plot(f_dec, X_dec(1:N/2));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Spectrum of Compressed Speech');
grid on;
plot:

Spectrum of Compressed Speech


1

0.8

0.6

0.4

0.2
Magnitude

-0.2

-0.4

-0.6

-0.8

-1
0 2000 4000 6000 8000 10000 12000
Frequency (Hz)
Speech Reconstruction Using Interpolation
Code:
clc; clear; close all;
[x, Fs] = audioread('[Link]');
x = x(:,1);
M = 2;
h = fir1(80, (Fs/(2*M))/(Fs/2));
x_dec = downsample(filter(h,1,x), M);
% Interpolation
x_up = upsample(x_dec, M);
x_rec = filter(h, 1, x_up);
% Match original length
x_rec = x_rec(1:length(x));
t = (0:length(x)-1)/Fs;
figure;
plot(t, x_rec);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Reconstructed Speech Signal');
grid on;
plot:

Reconstructed Speech Signal


0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)
Comparison of Original, Compressed & Reconstructed
Code:
clc; clear; close all;

% Read speech signal


[x, Fs] = audioread('[Link]');
x = x(:,1);

% Parameters
M = 2;
h = fir1(80, (Fs/(2*M))/(Fs/2));

% Compression (Decimation)
x_dec = downsample(filter(h,1,x), M);

% Reconstruction (Interpolation)
x_rec = filter(h,1, upsample(x_dec, M));
x_rec = x_rec(1:length(x));

% -------- Figure 1: Original Speech --------


figure;
plot(x);
title('Original Speech Signal');
xlabel('Samples');
ylabel('Amplitude');
grid on;

% -------- Figure 2: Compressed Speech --------


figure;
plot(x_dec);
title('Compressed Speech Signal (Decimated)');
xlabel('Samples');
ylabel('Amplitude');
grid on;

% -------- Figure 3: Reconstructed Speech --------


figure;
plot(x_rec);
title('Reconstructed Speech Signal');
xlabel('Samples');
ylabel('Amplitude');
grid on;
Plots:
Original Speech Signal
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 5 10 15
Samples 10 4

Compressed Speech Signal (Decimated)


1.5

0.5
Amplitude

-0.5

-1
0 1 2 3 4 5 6 7 8
Samples 4
10
Reconstructed Speech Signal
0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6
0 5 10 15
Samples 10 4
Difference (Error) Signal
Code:
clc; clear; close all;

[x, Fs] = audioread('[Link]');


x = x(:,1);

M = 2;
h = fir1(80, (Fs/(2*M))/(Fs/2));

% Reconstruction
x_rec = filter(h,1, upsample(downsample(filter(h,1,x),M),M));
x_rec = x_rec(1:length(x));

% Error
error_signal = x - x_rec;
t = (0:length(x)-1)/Fs;

figure;
plot(t, x_rec, 'b'); hold on;
plot(t, 20*error_signal, 'r');
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Reconstructed Signal and Magnified Error');
legend('Reconstructed Speech','Magnified Error');
grid on;

Plot:

Energy Comparison
Code:
clc; clear; close all;

[x, Fs] = audioread('[Link]');


x = x(:,1);

M = 2;
h = fir1(80, (Fs/(2*M))/(Fs/2));

x_rec = filter(h,1, upsample(downsample(filter(h,1,x),M),M));


x_rec = x_rec(1:length(x));

E_original = sum(x.^2);
E_reconstructed = sum(x_rec.^2);

figure;
bar([E_original E_reconstructed]);
set(gca,'XTickLabel',{'Original','Reconstructed'});
ylabel('Energy');
title('Energy Comparison');
grid on;

Plot:

Energy Comparison
3500

3000

2500

2000
Energy

1500

1000

500

0
Original Reconstructed

Audio Playback
Code:
clc; clear; close all;

% ---------------- Read Speech ----------------


[x, Fs] = audioread('[Link]');
x = x(:,1); % Convert to mono
t = (0:length(x)-1)/Fs; % Time axis

% ---------------- Parameters ----------------


M = 2; % Decimation factor
h = fir1(80, (Fs/(2*M))/(Fs/2)); % Anti-aliasing filter

% ---------------- Compression ----------------


x_filt = filter(h,1,x);
x_dec = downsample(x_filt, M);
Fs_dec = Fs/M;
t_dec = (0:length(x_dec)-1)/Fs_dec;

% ---------------- Reconstruction ----------------


x_up = upsample(x_dec, M);
x_rec = filter(h,1,x_up);
x_rec = x_rec(1:length(x));

% ---------------- Error Signal ----------------


error_signal = x - x_rec;
% FIGURE 1
% Original Speech
figure;
plot(t, x);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Original Speech Signal');
grid on;

% FIGURE 2
% Compressed Speech
figure;
plot(t_dec, x_dec);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Compressed Speech Signal (Decimated)');
grid on;

% FIGURE 3
% Reconstructed Speech
figure;
plot(t, x_rec);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Reconstructed Speech Signal');
grid on;

% FIGURE 4
% Original vs Reconstructed (Overlay)
figure;
plot(t, x, 'b'); hold on;
plot(t, x_rec, 'r--');
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Comparison: Original vs Reconstructed Speech');
legend('Original','Reconstructed');
grid on;

%FIGURE 5
% Difference (Error) Signal - Magnified
figure;
plot(t, 20*error_signal);
xlabel('Time (seconds)');
ylabel('Error Amplitude (Magnified)');
title('Difference Signal (Original ? Reconstructed)');
grid on;

% ---------------- Audio Playback (Optional) ----------------


disp('Playing Original Speech');
sound(x, Fs);
pause(3);

disp('Playing Compressed Speech');


sound(x_dec, Fs_dec);
pause(3);

disp('Playing Reconstructed Speech');


sound(x_rec, Fs);

Plots:
Original Speech Signal
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)

Compressed Speech Signal (Decimated)


1.5

0.5
Amplitude

-0.5

-1
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)
Reconstructed Speech Signal
0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)

Comparison: Original vs Reconstructed Speech


1
Original
0.8 Reconstructed

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)

You might also like