-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitch_shifter_test.m
203 lines (169 loc) · 5.46 KB
/
pitch_shifter_test.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
% <------------ NUMBER GENERATION ------------>
% Script for 24-bit random number generator
% Generates 100 24-bit random binary numbers
% Writes randomly-generated numbers to file in_data
in_data = fopen('./in_data', 'w');
for i = 1:100
random= randi([0,1],1, 24);
print_format = [repmat('%g', 1, numel(random)-1), '%g\n'];
fprintf(in_data, print_format, random);
end
fclose(in_data);
% <------------ SETUP ------------>
% Circular buffer length
l = 1024;
% Shifting factor
% 0.8 for low pitch
% 1 for normal voice
% 1.65 for high pitch
shift_factor = 0.8;
% Sample number
i = 0;
% ------------- BUFFERS AND POINTERS ------------- %
% Create 2 buffers for left data, and two buffers for right data
% Initialize buffers to zero
l_buf_0 = strings([1, l]);
l_buf_1 = strings([1, l]);
r_buf_0 = strings([1, l]);
r_buf_1 = strings([1, l]);
% Read and write pointers for l_buf_0, l_buf_1, r_buf_0, and r_buf_1
% Initialize w_0 to 1. MATLAB arrays start at 1.
% Initialize w_1 to 180 deg shifted cell.
% l_buf write
l_w_0 = 1;
l_w_1 = floor(l/2);
% l_buf read
l_r_0 = 1;
l_r_1 = 1;
% r_buf write
r_w_0 = 1;
r_w_1 = floor(l/2);
% r_buf read
r_r_0 = 1;
r_r_1 = 1;
% Indexing for read pointers
l_i_0= 0;
l_i_1= 0;
r_i_0= 0;
r_i_1= 0;
% Reads 2 binary numbers from in_data file
% First binary number: in_left
% Second binary number: in_right
% "Streams" them into the pitch_shifter function
in_data = fopen('./in_data', 'r');
out_data = fopen('./out_data','w');
in_left = fgetl(in_data); % Read the left data
in_right = fgetl(in_data); % Read the right data
while ~isequal(in_left, -1) && ~isequal(in_right, -1)
% Computes sum and writes to data_out
[out_left, out_right] = pitch_shifter(l, shift_factor, in_left, in_right, ...
i, l_buf_0, l_buf_1, l_w_0, ...
l_w_1, l_r_0, l_r_1, r_buf_0, r_buf_1,r_w_0, r_w_1, r_r_0, ...
r_r_1, l_i_0, l_i_1, r_i_0, r_i_1);
print_format = [repmat('%g', 1, numel(out_left)-1), '%g\n'];
fprintf(out_data, print_format, out_left);
print_format = [repmat('%g', 1, numel(out_right)-1), '%g\n'];
fprintf(out_data, print_format, out_right);
in_left = fgetl(in_data);
in_right = fgetl(in_data);
end
fclose(in_data);
fclose(out_data);
% Converts binary number to a matlab array
function[in_arr] = to_array(in)
in_arr = char(num2cell(convertStringsToChars(in)));
in_arr = reshape(str2num(in_arr),1,[]); %#ok<ST2NM>
end
function[out] = add(in_0, in_1)
% Function for 24-bit adder
% Takes 2 24-bit numbers
% Computes bit-wise addition
if (isvector(in_0) && length(in_0)==24) && (isvector(in_1) && length(in_1)==24)
out = zeros(1,24);
carry_out = 0;
for i = 24:-1:1
out(i) = in_0(i) + in_1(i) + carry_out;
if out(i) == 2
out(i) = 0;
carry_out = 1;
elseif out(i) == 3
out(i) = 1;
carry_out = 1;
else
carry_out = 0;
end
end
else
error("The given inputs are not 24-bit vectors");
end
end
function[out] = shift(in)
out = zeros(size(in));
out(2:24) = in(1:23);
end
% ------------- ALGORITHM ------------- %
% Read two samples at a time
% Add to buffers
% Read from buffers
% Take average
% Output to file
function [out_left, out_right] = pitch_shifter(l, shift_factor, in_left, in_right, i, ...
l_buf_0, l_buf_1, l_w_0, l_w_1, l_r_0, l_r_1, r_buf_0, r_buf_1, ...
r_w_0, r_w_1, r_r_0, r_r_1, l_i_0, l_i_1, r_i_0, r_i_1)
% LEFT BUFFER
% Add the sample to l_buf_0 at address l_w_0
% Advance l_w_0
l_buf_0(l_w_0) = in_left;
l_w_0 = mod(i, l) + 1;
% Add the sample to l_buf_1
% Advance l_w_1
l_buf_1(l_w_1) = in_left;
l_w_1 = mod(i+floor(l/2), l) + 1;
% Read from l_buf_0 at address l_r_0
l_i_0= l_i_0 + shift_factor;
l_r_0 = mod(floor(l_i_0), l) + 1;
% Read from l_buf_2 at address l_r_1
l_i_1= l_i_1 + shift_factor;
l_r_1 = mod(floor(l_i_1), l) + 1;
% RIGHT BUFFER
% Add the sample to l_buf_1 at address l_w_0
% Advance l_w_0
r_buf_0(r_w_0) = in_right;
r_w_0 = mod(i, l) + 1;
% Add the sample to l_buf_1
% Advance l_w_1
r_buf_1(r_w_1) = in_right;
r_w_1 = mod(i + floor(l/2), l) + 1;
% Read from l_buf_1 at address l_r_1
r_i_0= r_i_0 + shift_factor;
r_r_0 = mod(floor(r_i_0), l) + 1;
% Read from l_buf_2 at address l_r_2
r_i_1= r_i_1 + shift_factor;
r_r_1 = mod(floor(r_i_1), l) + 1;
% Adjust left values
if (l_buf_0(l_r_0)=="")
l_r_0_out = "000000000000000000000000";
else
l_r_0_out = l_buf_0(l_r_0);
end
if (l_buf_1(l_r_1)=="")
l_r_1_out = "000000000000000000000000";
else
l_r_1_out = l_buf_1(l_r_1);
end
% Adjust right values
if (r_buf_0(r_r_0)=="")
r_r_0_out = "000000000000000000000000";
else
r_r_0_out = r_buf_0(r_r_0);
end
if (r_buf_1(r_r_1)=="")
r_r_1_out = "000000000000000000000000";
else
r_r_1_out = r_buf_1(r_r_1);
end
out_left = add(to_array(l_r_0_out), to_array(l_r_1_out));
out_left = shift(out_left);
out_right = add(to_array(r_r_0_out), to_array(r_r_1_out));
out_right = shift(out_right);
end