-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpc_test.m
More file actions
120 lines (100 loc) · 3.74 KB
/
Copy pathlpc_test.m
File metadata and controls
120 lines (100 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
%% DAAP course 2025 - Homework 1: LPC-based Speak and Spell
% Implement the LPC10 speech encoding used in 1978 Speak and Spell toy by
% Texas Instruments. The TMC0280 chip used to synthesize speech using LPC.
%
% DAAP HW1 2025
% Mirco Pezzoli
% Di Lorenzo Giuliano
% Longhi Filippo
clc; clear; close all;
addpath('functions')
set(groot, 'DefaultFigureWindowStyle', 'docked', ... % all figures as tabs in single window
'DefaultTextInterpreter', 'latex', ... % interpreter Latex - text and annotations
'DefaultAxesTickLabelInterpreter', 'latex', ... % interpreter Latex - tick labels
'DefaultLegendInterpreter', 'latex', ... % interpreter Latex - legends
'DefaultLineLineWidth', 1.5, ... % functions
'DefaultAxesFontSize', 12, ... % axis and title
'DefaultTextFontSize', 14, ... % sgtitle
'DefaultAxesFontName', 'Times New Roman', ... % axis and title
'DefaultTextFontName', 'Times New Roman', ... % sgtitle
'DefaultAxesLineWidth', 1, ... % axis
'DefaultConstantLineLineWidth', 1.2, ... % xline and yline
'DefaultAxesTitleFontSizeMultiplier', 1.2, ... % title
'DefaultFigureColor', 'w', ... % background color
'DefaultAxesBox', 'on', ... % plot box
'DefaultLegendLocation', 'best' ... % legend position
);
%% Select an audio file
% Audio file path
filename = input("Choose file name (with right extension): ", "s");
disp("================================");
disp("Reading: " + filename);
[s, sr] = audioread("input/" + filename); % read audio file
s = mean(s, 2); % from stereo to mono
fs = 8e3; % work with sampling rate 8 KHz
s = resample(s, fs, sr); % resampling original audio
s = s./max(abs(s)); % normalization
disp("Reading complete");
disp("================================");
%% LPC encoding
encoder;
%% LPC decoding
decoder;
%% Comparison and results
% ------------------ PLOTS SECTION ------------------
if plot_bool
% Plot audio file
figure(999)
sgtitle("File " + filename + " - Waveforms and spectra")
t = (0 : length(s)-1) / fs;
subplot(2, 2, 1)
plot(t,s)
title("Original file - time domain")
xlabel("$t$ [s]")
ylabel("$s$")
xlim([min(t) max(t)])
ylim([-1 1])
grid on
N = length(s);
S = fft(s);
f = (0:N-1) * (fs / N);
subplot(2, 2, 2)
plot(f(1:floor(N/2)), db(abs(S(1:floor(N/2)))))
title("Original file - frequency domain")
xlabel("$f$ [Hz]")
ylabel("$|S|$ [dB]")
grid on
t = (0:length(s_rec)-1) / fs;
subplot(2, 2, 3)
plot(t, s_rec);
title("Synthesized file - time domain")
xlabel("$t$ [s]")
ylabel("$\hat{s}$")
xlim([min(t) max(t)])
ylim([-1 1])
grid on
N = length(s_rec);
S = fft(s_rec);
f = (0:N-1) * (fs / N);
subplot(2, 2, 4)
plot(f(1:floor(N/2)), db(abs(S(1:floor(N/2)))));
title("Synthesized file - frequency domain")
xlabel("$f$ [Hz]")
ylabel("$|\hat{S}|$ [dB]")
grid on
end
% ------------------ END PLOTS SECTION ------------------
disp("================================");
disp("Player: " + filename);
if lower(input("Would you like to play the original signal? (y/n): ", 's')) == 'y'
disp("Playing the original file")
soundsc(s, fs);
pause(length(s) / fs);
end
if lower(input("Would you like to play the synthesized signal? (y/n): ", 's')) == 'y'
disp("Playing the synthesized file")
soundsc(s_rec, fs);
pause(length(s_rec) / fs);
end
disp("Player complete");
disp("================================");