diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/0.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/0.wav" new file mode 100644 index 000000000..52a82c193 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/0.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/1.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/1.wav" new file mode 100644 index 000000000..5722d86ea Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/1.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/10.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/10.wav" new file mode 100644 index 000000000..2f0cf8c69 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/10.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/11.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/11.wav" new file mode 100644 index 000000000..761b7986d Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/11.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/18217569099.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/18217569099.wav" new file mode 100644 index 000000000..bc404132c Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/18217569099.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/2.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/2.wav" new file mode 100644 index 000000000..cbf851d03 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/2.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/3.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/3.wav" new file mode 100644 index 000000000..d05bced72 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/3.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/4.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/4.wav" new file mode 100644 index 000000000..2ef53087a Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/4.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/5.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/5.wav" new file mode 100644 index 000000000..8b62099ef Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/5.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/6.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/6.wav" new file mode 100644 index 000000000..54dea6bf8 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/6.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/7.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/7.wav" new file mode 100644 index 000000000..b3cd00b54 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/7.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/8.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/8.wav" new file mode 100644 index 000000000..41b23a483 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/8.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/9.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/9.wav" new file mode 100644 index 000000000..acb12e517 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/data/9.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/ex1.log" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/ex1.log" new file mode 100644 index 000000000..c5c96c595 --- /dev/null +++ "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/ex1.log" @@ -0,0 +1,20 @@ +Single button test +|length |fft-result |fft-time |grt-result |grt-time |time ratio | +| ---- | ---- | ---- | ---- | ---- | ---- | +|25695 | 0 |0.002852 | 0 |0.001258 | 2.267032 | +|14988 | 1 |0.000599 | 1 |0.000753 | 0.796466 | +|19985 | 2 |0.004619 | 2 |0.001288 | 3.586335 | +|19271 | 3 |0.001731 | 3 |0.000950 | 1.822278 | +|19985 | 4 |0.001775 | 4 |0.001007 | 1.762288 | +|23018 | 5 |0.002294 | 5 |0.001170 | 1.959672 | +|19271 | 6 |0.001924 | 6 |0.000983 | 1.956478 | +|23018 | 7 |0.000978 | 7 |0.001117 | 0.875862 | +|18736 | 8 |0.000715 | 8 |0.001087 | 0.657708 | +|22483 | 9 |0.002365 | 9 |0.001126 | 2.100906 | +|23019 | * |0.002585 | * |0.001172 | 2.205220 | +|24625 | # |0.002101 | # |0.001710 | 1.228426 | +telephone number +FFT time_cost: 0.034963 s +ans: ---8-22-1177-5-6699-0-99-99 +Goertzel time_cost: 0.021653 s +ans: 1--8-22-1177-5-6699-0099-99 diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/main.m" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/main.m" new file mode 100644 index 000000000..3de40e2ac --- /dev/null +++ "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex1/main.m" @@ -0,0 +1,125 @@ +% 主函数 +function main + global keys; % 按键表 + global freqs; % 频率表 + keys = ['1', '2', '3', 'A'; + '4', '5', '6', 'B'; + '7', '8', '9', 'C'; + '*', '0', '#', 'D'; + '-', '-', '-', '-']; + freqs = [697, 770, 852, 941, 1209, 1336, 1477, 1633]; + + % 输出到文件 ex1.log + fout = fopen('./ex1.log', 'w'); + + % 单按键测试 + fprintf(fout, "Single button test\n"); + fprintf(fout, "|length |fft-result |fft-time |grt-result |grt-time |time ratio |\n"); + fprintf(fout, "| ---- | ---- | ---- | ---- | ---- | ---- |\n"); + for i = 0:11 + [audio, fs] = audioread(['./data/',num2str(i),'.wav']); + audio = sum(audio, 2) / 2; % 合并左右声道 + % audio = audio(1:4000); + fprintf(fout, "|%5d ", length(audio)); + tic + ans = fft_test(audio, fs); + fft_time = toc; + fprintf(fout, "| %c |%f ", ans, fft_time); + tic + ans = goertzel(audio, fs); + grt_time = toc; + fprintf(fout, "| %c |%f ", ans, grt_time); + fprintf(fout, "| %f |", fft_time/grt_time); + fprintf(fout, "\n"); + end + + % 一组电话号码测试 + [audio, fs] = audioread('./data/18217569099.wav'); + patch_size = fs/3; + audio = sum(audio, 2) / 2; + total_len = size(audio, 1); + + fprintf(fout, "telephone number\n"); + fft_result = []; + tic + for i = 1:patch_size:total_len-patch_size + fft_result = [fft_result, fft_test(audio(i:i+patch_size), fs)]; + end; + fprintf(fout, "FFT time_cost: %f s\n", toc); + fprintf(fout, "ans: %s\n", fft_result); + + grt_result = []; + tic + for i = 1:patch_size:total_len-patch_size + grt_result = [grt_result, goertzel(audio(i:i+patch_size), fs)]; + end; + fprintf(fout, "Goertzel time_cost: %f s\n", toc); + fprintf(fout, "ans: %s\n", grt_result); + + fclose(fout); +end + +% FFT Algorithm +function ans = fft_test(audio, fs) + global keys; + global freqs; + + y = abs(fft(audio)); + len = length(y); + % 数组下标是数字频率,需要转换成模拟频率 + l1 = round(650 / fs * len); + h1 = round(1000 / fs * len); + l2 = round(1150 / fs * len); + h2 = round(1700 / fs * len); + [px, col] = max(y(l2:h2)); + [py, row] = max(y(l1:h1)); + col = col + l2 - 1; + row = row + l1 - 1; + fx = col / len * fs; + fy = row / len * fs; + + ans = '-'; + if (px > 10 || py > 10) % 过滤噪声 + [t, row] = min(abs(freqs(1:4) - fy)); + [t, col] = min(abs(freqs(5:8) - fx)); + ans = keys(row, col); + end; +end + +% Goertzel Algorithm +function ans = goertzel(audio, fs) + global keys; + global freqs; + + t_freqs = freqs; + N = length(audio); + x = audio; + v = zeros(1,N+2); + y = zeros(1, 8); + t_freqs = round(t_freqs * N / fs); + + % 差分方程 + % v_k[n] = 2cos(2k\pi/N)v_k[n-1] - v_k[n-2] + x[n] + % y_k[n]^2 = v_k[n] - W_N^k*v_k[n-1] + for k = 1 : length(t_freqs) + factor = 2 * cos(2 * pi * t_freqs(k) / N); + for i = 1 : N + v(i+2) = factor*v(i+1) - v(i) + x(i); % v_k[n] + end + y(k) = v(N+2)^2 + v(N+1)^2 - factor*v(N+2)*v(N+1); % |y_k[N]|^2 + end + y = sqrt(abs(y)); + + % 过滤噪声 + [t, col] = max(y(5:8)); + [t, row] = max(y(1:4)); + px = y(col+4); + py = y(row); + trhld = max(px, py)*0.3; + if (sum((y-trhld) > 0) > 2) + row = 5; + end + + ans = keys(row, col); +end + diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex2/main.m" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex2/main.m" new file mode 100644 index 000000000..54fcbfe9d --- /dev/null +++ "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex2/main.m" @@ -0,0 +1,98 @@ +function main + points = 400; + step = 100; + M = 1000; % length y + time_origin = zeros(1, points); + time_circle = zeros(1, points); + time_save = zeros(1, points); + time_add = zeros(1, points); + + for i = 1 : points + N = M + (i-1) * step + fprintf("testing N = %d, M = %d\n", N, M); + x = rand([1, N]); + y = rand([1, M]); + tic; + ans1 = conv_origin(x, y); + time_origin(i) = toc; + tic; + ans2 = conv_circle(x, y); + time_circle(i) = toc; + tic; + ans3 = overlap_save(x, y); + time_save(i) = toc; + tic; + ans4 = overlap_add(x, y); + time_add(i) = toc; + end + x = M:step:M + step*(points-1); + + plot(x, time_origin, '-', x, time_circle, '-', x, time_save, '-', x, time_add, '-'); + legend({'origin conv', 'circle conv', 'overlap-save', 'overlap-add'}, 'Location', 'northwest'); + xlabel('M'); + ylabel('time_cost/s'); +end + +function ans = conv_origin(x, y) + N = length(x); + M = length(y); + L = M + N - 1; + ans = zeros(1, L); + + for n = 1 : L + m0 = max(1, n + 1 - N); + m1 = min(M, n); + for m = m0 : m1 + ans(n) = ans(n) + x(n - m + 1) * y(m); + end + end +end + +function ans = conv_circle(x, y) + N = length(x); + M = length(y); + x = [x, zeros(1, M-1)]; % zero padding + y = [y, zeros(1, N-1)]; % zero padding + ans = ifft(fft(x) .* fft(y)); +end + +function y = overlap_add(x, h) + N = length(x); + M = length(h); + L = M + 1; % block size + len = L + M - 1; + fft_h = fft(h, len); + num = ceil(N / L); % split x into $num blocks + y = zeros(1, (num + 1) * L); + x = [x, zeros(1, (num + 1) * L - N)]; % zero padding to (num + 1) * L + ol = zeros(1, M - 1); % overlap-add + for i = 1:L:num * L + 1 + y_k = ifft(fft(x(i:i + L - 1), len) .* fft_h); % calc circle conv + y_k(1:M - 1) = y_k(1:M - 1) + ol(1:M - 1); % overlap-add + ol(1:M - 1) = y_k(L + 1:len); % refresh + y(i:i + L - 1) = y_k(1:L); % save y + end + y = y(1:N + M - 1); +end + +% conv by overlap-save +function y = overlap_save(x, h) + % assume h is much shorter than x + N = length(x); + M = length(h); + L = M + 1; % block size + len = L + M - 1; + fft_h = fft(h, len); + num = ceil(N / L); % split x into $num blocks + y = zeros(1, (num + 1) * L); + x = [x, zeros(1, (num + 1) * L - N)]; % zero padding + ol = zeros(1, M - 1); % overlap-save + for i = 1:L:num * L + 1 + subx = [ol, x(i:i + L - 1)]; + ol = subx(L + 1 : L + M - 1); % refresh overlap-save + y_k = ifft(fft(subx, len) .* fft_h); % calc circle conv + y(i:i + L - 1) = y_k(M:M + L - 1); % save y + end + y = y(1:N + M - 1); +end + diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/1.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/1.wav" new file mode 100644 index 000000000..f268cfb2e Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/1.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/2.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/2.wav" new file mode 100644 index 000000000..9d71c5bfe Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/2.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/3.wav" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/3.wav" new file mode 100644 index 000000000..1d66530fb Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/data/3.wav" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/main.m" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/main.m" new file mode 100644 index 000000000..a50982cb7 --- /dev/null +++ "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/ex3/main.m" @@ -0,0 +1,96 @@ +% 读取数据 +[audio1, fs] = audioread('./data/1.wav'); +[audio2, fs] = audioread('./data/2.wav'); +[audio3, fs] = audioread('./data/3.wav'); +fprintf("read freq: %dHz\n", fs); +total_len = 150000; % 取前150000个点 +audio1 = audio1(1 : total_len); +audio2 = audio2(1 : total_len); +audio3 = audio3(1 : total_len); + +% 原始的时域信号 +t = 0 : total_len - 1; +figure(1); +subplot(311); +stem(t, audio1, '.'); +subplot(312); +stem(t, audio2, '.'); +subplot(313); +stem(t, audio3, '.'); + +% 原始的频域信号 +figure(2); +subplot(311); +stem(t, abs(fft(audio1)), '.'); +subplot(312); +stem(t, abs(fft(audio2)), '.'); +subplot(313); +stem(t, abs(fft(audio3)), '.'); + +% 1/3抽取和三倍上采样的下标 +sample_idx = 1 : 3 : total_len - 1; + +% 在时域进行1/3抽取 +audio1 = audio1(sample_idx); +audio2 = audio2(sample_idx); +audio3 = audio3(sample_idx); + +% 时域上进行上采样 +a1 = zeros(1, total_len); +a2 = zeros(1, total_len); +a3 = zeros(1, total_len); +a1(sample_idx) = audio1; +a2(sample_idx) = audio2; +a3(sample_idx) = audio3; + +% 变换到频域 +f1 = fft(a1); +f2 = fft(a2); +f3 = fft(a3); + +% f是频域编码信号 +len = total_len / 6; +f = zeros(1, total_len); +f(1:len*3) = [f1(1:len), f2(1:len), f3(1:len)]; +f(len*3+1:len*6) = [f3(len+1:len*2), f2(len+1:len*2), f1(len+1:len*2)]; + +% y是编码完成后的时域信号 +y = ifft(f); +t = 0:total_len-1; +figure(3); +subplot(211); +stem(t, abs(f), '.'); +subplot(212); +stem(t, real(y), '.'); + +% 频域解码 +f = fft(y); +f1 = [f(1:len), f(len*5+1:len*6)]; +f2 = [f(len+1:len*2), f(len*4+1:len*5)]; +f3 = [f(len*2+1:len*3), f(len*3+1:len*4)]; +t = 0:2*len-1; +figure(4); +subplot(311); +stem(t, abs(f1), '.'); +subplot(312); +stem(t, abs(f2), '.'); +subplot(313); +stem(t, abs(f3), '.'); + +% 恢复时域信号 +audio1 = real(ifft(f1)); +audio2 = real(ifft(f2)); +audio3 = real(ifft(f3)); +a1 = zeros(1, total_len); +a2 = zeros(1, total_len); +a3 = zeros(1, total_len); +a1(sample_idx) = audio1; +a2(sample_idx) = audio2; +a3(sample_idx) = audio3; +figure(5); +subplot(311); +stem(1:total_len, a1, '.'); +subplot(312); +stem(1:total_len, a2, '.'); +subplot(313); +stem(1:total_len, a3, '.'); diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex2.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex2.jpg" new file mode 100644 index 000000000..5a6e82f07 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex2.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-1.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-1.jpg" new file mode 100644 index 000000000..581abbc80 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-1.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-2.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-2.jpg" new file mode 100644 index 000000000..841213931 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-2.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-3.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-3.jpg" new file mode 100644 index 000000000..76903352b Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-3.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-4.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-4.jpg" new file mode 100644 index 000000000..e2f96bfa2 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-4.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-5.jpg" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-5.jpg" new file mode 100644 index 000000000..228a98633 Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/ex3-5.jpg" differ diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/\344\277\241\345\216\237\346\212\245\345\221\212.md" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/\344\277\241\345\216\237\346\212\245\345\221\212.md" new file mode 100644 index 000000000..d8f6f10b6 --- /dev/null +++ "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/report/\344\277\241\345\216\237\346\212\245\345\221\212.md" @@ -0,0 +1,135 @@ +## 信号处理原理实验报告 + +> 陈海天 计73 2016010106 + +### Ex1 双音频按键识别 + +##### 1.1 算法与代码 + +使用了两种不同的方法来识别按键音: + +1. 手动实现goertzel算法,在八个频率中找出最高的两个峰值作为预测结果(按一定条件过滤噪声) +2. 使用matlab内置的fft函数,找出低频高频的两个峰值查表作为预测结果 + +在main.m中写了两个函数`fft_test` `goertzel`分别实现上面两种方法,运行main.m即可得到结果。 + +##### 1.2 实验结果 + +1. 单按键测试 + +用自己手机录了键盘上的12个按键音,放在data目录下(11--*,12--#),采样频率fs=48000,样本的长度在半秒左右。N取4000的结果如下:(其中time_ratio为fft_time/grt_time) + +|length |fft-result |fft-time |grt-result |grt-time |time ratio | +| ---- | ---- | ---- | ---- | ---- | ---- | +| 4000 | 0 |0.000207 | 0 |0.000319 | 0.649937 | +| 4000 | 1 |0.000170 | 1 |0.000238 | 0.717172 | +| 4000 | 2 |0.000146 | 2 |0.000237 | 0.617138 | +| 4000 | 3 |0.000149 | 3 |0.000234 | 0.633532 | +| 4000 | 4 |0.000151 | 4 |0.000237 | 0.640592 | +| 4000 | 5 |0.000151 | 5 |0.000237 | 0.637785 | +| 4000 | 6 |0.000146 | 6 |0.000293 | 0.498120 | +| 4000 | 7 |0.000150 | 7 |0.000305 | 0.491148 | +| 4000 | 8 |0.000148 | 8 |0.000236 | 0.628450 | +| 4000 | 9 |0.000157 | 9 |0.000235 | 0.669365 | +| 4000 | * |0.000147 | * |0.000236 | 0.620237 | +| 4000 | # |0.000152 | # |0.000236 | 0.644161 | + +N取样本长度时的结果如下: +|length |fft-result |fft-time |grt-result |grt-time |time ratio | +| ---- | ---- | ---- | ---- | ---- | ---- | +|25695 | 0 |0.002802 | 0 |0.001537 | 1.823246 | +|14988 | 1 |0.000697 | 1 |0.000871 | 0.799656 | +|19985 | 2 |0.001747 | 2 |0.001007 | 1.735525 | +|19271 | 3 |0.001737 | 3 |0.001216 | 1.429006 | +|19985 | 4 |0.001487 | 4 |0.000947 | 1.570976 | +|23018 | 5 |0.001132 | 5 |0.001186 | 0.955048 | +|19271 | 6 |0.001650 | 6 |0.000914 | 1.803937 | +|23018 | 7 |0.001307 | 7 |0.001161 | 1.125560 | +|18736 | 8 |0.000736 | 8 |0.000890 | 0.826947 | +|22483 | 9 |0.002732 | 9 |0.001115 | 2.449646 | +|23019 | * |0.002042 | * |0.001083 | 1.885400 | +|24625 | # |0.001880 | # |0.001597 | 1.177458 | + +可见goertzel的准确性是没问题的。从性能上来说,当N大时,goertzel算法速度更快。这与理论结果相符,goertzel算法计算K个点的时间复杂度为O(KN),而fft的时间复杂度为O(NlogN),所以N越大,goertzel更有优势。 + +2. 长音频测试 + +录了一段连续的长音频(电话号码),N分别取fs=48000, fs/3=16000, fs/6=8000,可得到结果如下: + +``` +'-'表示噪声,噪声筛除方式: +goertzel: 如果大于[0.3*最大能量]的点超过两个,则认为是噪声 +fft: 如果能量小于一定阈值,则认为是噪声 + +N = 48000 +FFT time_cost: 0.044336 s +ans: -82756#99 +Goertzel time_cost: 0.022292 s +ans: 18--56-99 + +N = 16000 +FFT time_cost: 0.034408 s +ans: ---8-22-1177-5-6699-0-99-99 +Goertzel time_cost: 0.022509 s +ans: 1--8-22-1177-5-6699-0099-99 + +N = 8000 +FFT time_cost: 0.035877 s +ans: ------88---22----11--777--55---66--999---0---99---999- +Goertzel time_cost: 0.023650 s +ans: 11----88---22----11--777--55---66#-999--000--99---999# +``` + +可见goertzel的准确性非常好,和fft不相上下,而且所用时间基本不变,因为它是线性关系。而fft的时间随batch_size的减小而减小。 + + + +### Ex2 卷积计算方法的性能比较 + +按照要求用以下四种方式计算卷积:`公式法` `圆卷积` `overlap-add` `overlap-save` + +所得的实验结果如下图所示(取了400个点,步长100,h的长度M=1000): + +![](ex2.jpg) + +可见手搓的公式法求解速度最慢(其实内置函数的公式法是最快的,但是这应该是matlab在底层做了优化);其次是圆卷积fft;两个overlap方法计算都很快,这是因为它们比较适合这种长短差异较大的序列的卷积求解。 + +### Ex3 语音信号的频分复用 + +本题有多种理解方式(感觉题目要求没太说清楚),我的理解方式是用同样长度的序列编码三条语音,并且恢复以后尽可能不失真。在这种理解方式下,有两种做法: + +1. 因为我们手机录的素材采样频率基本都在48k左右,而语音的带限是3.4k,所以可以直接在频域上截断三个信号然后拼起来。但是这种方式是有限制的,当采样频率小的时候就会严重失真。 +2. 第二种方式就是现在时域上进行1/3抽取,然后在按照ppt上的上采样--滤波--累加的方式。这样相当于在损失了一些音质,但是适用范围更广。 + +两种方式都会损失信息,因为想要用一条语音的数据量存下三条语言,肯定是要损失一些东西的。我最后采用的方式是第二种(因为这样也可以按照ppt上介绍的流程来)。 + +算法流程在代码注释中写的很详细了,大致为: + +1. 先将三条语音裁成同样长度,并进行1/3抽取 +2. 进行上采样,并fft得到上采样后的频域信号 +3. 在频域上编码 +4. 转换回时域 + +解码过程就是上述的逆过程。 + +#### 实验结果如下所示: + + + +上图为三条原始音频信号。 + + + +上图为三条原始音频的频谱。 + + + +上图为上采样之后三条音频的频谱,可见在一个fs内出现了三个周期。 + + + +上图为编码之后的频谱与音频信号。可见频谱上的确是三条信号分开了。 + + + +上图为解码之后恢复的音频信号。可见和原来的音频信号完全一致,试听效果也很好。 diff --git "a/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/\344\277\241\345\216\237\346\212\245\345\221\212.pdf" "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/\344\277\241\345\216\237\346\212\245\345\221\212.pdf" new file mode 100644 index 000000000..43be32abf Binary files /dev/null and "b/\345\244\247\344\270\211\344\270\212/\344\277\241\345\217\267\345\244\204\347\220\206\345\216\237\347\220\206/hw/matlab/2019/\344\277\241\345\216\237\346\212\245\345\221\212.pdf" differ