1
+ % Removing all variables, functions, and MEX-files from memory, leaving the
2
+ % workspace empty.
3
+ clear all
4
+
5
+
6
+ % Deleting all figures whose handles are not hidden.
7
+ close all
8
+
9
+
10
+ % Deleting all figures including those with hidden handles.
11
+ close all hidden
12
+
13
+
14
+ % Clearing all input and output from the Command Window display giving us a clean screen.
15
+ clc
16
+
17
+
18
+ % Opening the file 'TEOTH.mp3' in the read access mode.
19
+ fid = fopen (' TEOTH.mp3' ,' r' );
20
+
21
+
22
+ % Generating the input signal 'm(t)' by reading the binary data in 16 bit
23
+ % integer format from the specified file and writing it into a matrix
24
+ % 'm(t)'.
25
+ m = fread (fid ,' int16' );
26
+
27
+
28
+ % Defining the count for efficiency.
29
+ count = 8500 ;
30
+
31
+
32
+ % Calculating maximum value of the input signal 'm(t)'.
33
+ Mp = max (m )
34
+
35
+
36
+ % Setting number of bits in a symbol.
37
+ bits = 8 ;
38
+
39
+
40
+ % Defining the number of levels of uniform quantization.
41
+ levels = 2 ^ bits ;
42
+
43
+
44
+ % Calculating the step size of the quantization.
45
+ step_size = (2 * Mp )/levels
46
+
47
+
48
+ % Setting the sampling frequency.
49
+ % because the audio signal has a maximum frequency of 4K and according to
50
+ % Nyquist criteria, we get the following sampling frequency.
51
+ Fs = 8000 ;
52
+
53
+
54
+ % Setting the sampling instant.
55
+ Ts = 1 ;
56
+
57
+
58
+ % Setting the number of samples to be used.
59
+ No_Samples = (2 * Fs )+Ts ;
60
+
61
+
62
+ % Define the time vector for the calculations.
63
+ time = [1 : Fs / 64 ];
64
+
65
+
66
+ % Calculating the bit rate.
67
+ bit_rate = 8000 * bits ;
68
+
69
+
70
+ % Quantizing the input signal 'm(t)'.
71
+ for k = 1 : No_Samples ,
72
+ samp_in(k ) = m(k * Ts );
73
+ quant_in(k ) = samp_in(k )/step_size ;
74
+ error(k ) = (samp_in(k ) - quant_in(k ))/No_Samples ;
75
+ end
76
+
77
+
78
+ % Indicating the sign of the input signal 'm(t)' and calculating the
79
+ % quantized signal 'quant_out'.
80
+ signS = sign (m );
81
+ quant_out = quant_in ;
82
+ for i = 1 : count ,
83
+ S(i ) = abs (quant_in(i )) + 0.5 ;
84
+ quant_out(i ) = signS(i )*round(S(i ))*step_size ;
85
+ end
86
+
87
+
88
+ % Calculating the quantization noise 'Nq'.
89
+ Nq = ((Mp )^2 )/(3 *((levels )^2 ))
90
+
91
+
92
+ % Calculating signal to noise ratio 'SNR'.
93
+ SNR = 1.5 *((levels )^2 )
94
+ Gms = log10(SNR )
95
+
96
+
97
+ % Plotting the input signal 'm(t)'.
98
+ % figure;
99
+ subplot(4 ,1 ,1 );
100
+ plot(time ,m(time ));
101
+ title(' Message Signal' );
102
+ xlabel(' Time' );
103
+ ylabel(' m(t)' );
104
+ grid on ;
105
+
106
+
107
+ % Plotting the quantized signal 'quant_in(t)'.
108
+ % figure;
109
+ subplot(4 ,1 ,2 );
110
+ stem(time ,quant_in(time ),' r' );
111
+ title(' Quantized Speech Signal' );
112
+ xlabel(' Time' );
113
+ ylabel(' Levels' );
114
+ grid on ;
115
+
116
+
117
+ % Plotting the PCM signal 's_out(t)'.
118
+ % figure;
119
+ subplot(4 ,1 ,3 );
120
+ plot(time ,quant_out(time ));
121
+ title(' PCM Speech Signal' );
122
+ xlabel(' Time' );
123
+ ylabel(' PC Signal' );
124
+ grid on ;
125
+
126
+
127
+ % Plotting the error signal 'error(t)'.
128
+ subplot(4 ,1 ,4 );
129
+ plot(time ,error(time ));
130
+ title(' Error Signal' );
131
+ xlabel(' Time' );
132
+ ylabel(' Error(t)' );
133
+ grid on ;
134
+
135
+
136
+ % Removing all variables, functions, and MEX-files from memory, leaving the
137
+ % workspace empty.
138
+ clear all
0 commit comments