├── LICENSE ├── README.md ├── anc.ipynb ├── anc.m ├── binaural.ipynb ├── fourier_series_mov.m ├── ft.mp4 ├── ir_convolution.ipynb ├── lin_predict.ipynb ├── lin_predict.m ├── markov_proc_o1.m ├── neural_net.m ├── plane_wave.m ├── sph_harm.m ├── wiener_filter.ipynb └── wiener_filter.m /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Shoichi Koyama 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | teaching 2 | ==== 3 | ## Description 4 | Codes for teaching 5 | 6 | ## List 7 | - markov_proc.m: markov process of 1st order 8 | - lin_predict.m: linear prediction 9 | - wiener_filter.m: wiener filtering for speech enhancement 10 | - plane_wave.m: simulate plane wave 11 | - sph_harm.m: plot spherical harmonic function 12 | - anc.m: active noise control in 1D 13 | - neural_net.m: 2 layer neural network for handwritten digit recognition 14 | 15 | ## License 16 | [MIT](https://github.com/sh01k/lecture/blob/master/LICENSE) 17 | 18 | ## Author 19 | [Shoichi Koyama](http://www.sh01.org/) 20 | -------------------------------------------------------------------------------- /anc.m: -------------------------------------------------------------------------------- 1 | %% Parameters 2 | 3 | % Noise source position [m] 4 | x_ns = -2.0; 5 | 6 | % Reference mic position [m] 7 | x_rm = -1.0; 8 | 9 | % Loudspeaker 10 | %position [m] 11 | x_ls = 0; 12 | 13 | % Error mic position [m] 14 | x_em = 2.0; 15 | 16 | %Sound speed [m/s] 17 | c = 340.0; 18 | 19 | %Sampling frequency [Hz] 20 | fs = 16000; 21 | 22 | %FFT parameters 23 | fftlen = 1024; 24 | fftshift = fftlen/2; 25 | 26 | %Time [s] 27 | t = (1:fftshift)'/fs; 28 | 29 | %% 30 | 31 | %Noise source signal 32 | s_len = fs*8; 33 | s = rand(s_len,1)-0.5; 34 | 35 | %Impulse responses 36 | h_s2r = sinc((t-abs(x_rm-x_ns)/c)*fs)/abs(x_rm-x_ns); 37 | g = sinc((t-abs(x_em-x_ls)/c)*fs)/abs(x_em-x_ls); 38 | h_s2e = sinc((t-abs(x_em-x_ns)/c)*fs)/abs(x_em-x_ns); 39 | 40 | %Noise at reference mic 41 | x_s = conv(h_s2r,s); 42 | 43 | %Noise on error mic 44 | n = conv(h_s2e,s); 45 | 46 | %Number of frames 47 | num_frm = floor(length(x_s)/fftlen)-1; 48 | 49 | pw_e = zeros(num_frm,1); 50 | pw_n = zeros(num_frm,1); 51 | 52 | %Buffer 53 | d_t = zeros(fftshift,1); 54 | d_buff = zeros(fftshift,1); 55 | 56 | y_t = zeros(fftshift,1); 57 | y_buff = zeros(fftshift,1); 58 | 59 | %Initial filter 60 | h_est = zeros(fftshift,1); 61 | 62 | g_mat = convmtx(g,fftshift*2-1); 63 | norm2_g = norm(g_mat).^2; 64 | 65 | alpha = 0.04; 66 | beta = 1e-6; 67 | 68 | for ii=1:num_frm 69 | x_t = x_s((ii-1)*fftshift+1:ii*fftshift); 70 | n_t = n((ii-1)*fftshift+1:ii*fftshift); 71 | 72 | %Loudspeaker output 73 | dd_t = real(ifft(fft(h_est,fftlen).*fft(x_t,fftlen))); 74 | d_t = dd_t(1:fftshift)+d_buff; 75 | d_buff = dd_t(fftshift+1:fftlen); 76 | 77 | %Driving signal at error mic 78 | yy_t = real(ifft(fft([g; zeros(fftshift,1)],fftlen).*fft([d_t; zeros(fftshift,1)],fftlen))); 79 | y_t = yy_t(1:fftshift)+y_buff; 80 | y_buff = yy_t(fftshift+1:fftlen); 81 | 82 | %Signal at error mic 83 | e_t = y_t + n_t; 84 | 85 | pw_e(ii) = sum(abs(e_t).^2); 86 | pw_n(ii) = sum(abs(n_t).^2); 87 | 88 | %Filter update 89 | h_grad = 2*g_mat(1:2*fftshift-1,:)'*[e_t; zeros(fftshift-1,1)]*[x_t; zeros(fftshift-1,1)]'; 90 | norm2_x = sum(abs(x_t).^2); 91 | for jj=1:fftshift 92 | h_est = h_est - (alpha/(beta+norm2_g*norm2_x))*h_grad(jj:fftshift+jj-1,jj); 93 | end 94 | 95 | % figure(1); 96 | % plot(t,n_t,t,d_t); 97 | % xlim([t(1),t(fftshift)]); 98 | % ylim([-0.3,0.3]); 99 | % grid on; 100 | 101 | figure(2); 102 | plot(t,n_t,t,e_t); 103 | xlim([t(1),t(fftshift)]); 104 | ylim([-0.2,0.2]); 105 | grid on; 106 | 107 | drawnow; 108 | end 109 | 110 | figure(3); 111 | plot(1:num_frm,10*log10(pw_e./pw_n)); 112 | xlim([1,num_frm]); 113 | grid on; -------------------------------------------------------------------------------- /fourier_series_mov.m: -------------------------------------------------------------------------------- 1 | close all; 2 | clear variables; 3 | 4 | set(0,'defaultAxesFontSize',16); 5 | set(0,'defaultAxesFontName','Times'); 6 | set(0,'defaultTextFontSize',16); 7 | set(0,'defaultTextFontName','Times'); 8 | 9 | %Period [s] 10 | T = 1; 11 | 12 | %Sampling frequency for plot 13 | Fs = 100; 14 | 15 | %Duration [s] 16 | t_len = 4*T; 17 | t_num = floor(t_len*Fs); 18 | 19 | %Angle [rad] 20 | ang = linspace(0,2*pi,100); 21 | 22 | %Order 23 | k = [1,2,3,4]; 24 | k_mat = repmat(k,t_num,1); 25 | 26 | fig(1)=figure(1); 27 | fig_pos = [0, 800, 800, 800]; 28 | set(fig(1),'Position',fig_pos); 29 | 30 | v = VideoWriter('ft.mp4','MPEG-4'); 31 | open(v); 32 | 33 | for t0=0:399 34 | clf('reset'); 35 | 36 | %Time [s] 37 | t = (t0:t0+t_num-1)'/Fs; 38 | t_mat = repmat(t,1,length(k)); 39 | 40 | %Fourier coefficients 41 | c = -T./(pi*k).*cos(pi*k); %Saw wave 42 | 43 | %Fourier bases 44 | phi = 2*pi.*k_mat.*t_mat/T; 45 | circ = [c.*cos(phi(t_num,:)); c.*sin(phi(t_num,:))]; 46 | 47 | %Signal 48 | sig = sum(repmat(c,t_num,1).*sin(phi),2); 49 | 50 | %Plot complex plane 51 | subplot('Position',[0.04,0.85,0.14,0.14]); 52 | hold on; 53 | plot(c(1)*cos(ang),c(1)*sin(ang),'k','LineWidth',1.5); 54 | plot(c(2)*cos(ang)+circ(1,1),c(2)*sin(ang)+circ(2,1),'k','LineWidth',1.5); 55 | plot(c(3)*cos(ang)+circ(1,1)+circ(1,2),c(3)*sin(ang)+circ(2,1)+circ(2,2),'k','LineWidth',1.5); 56 | plot(c(4)*cos(ang)+circ(1,1)+circ(1,2)+circ(1,3),c(4)*sin(ang)+circ(2,1)+circ(2,2)+circ(2,3),'k','LineWidth',1.5); 57 | plot([0,circ(1,1)],[0,circ(2,1)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 58 | plot([circ(1,1),circ(1,2)+circ(1,1)],[circ(2,1),circ(2,2)+circ(2,1)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 59 | plot([circ(1,2)+circ(1,1),circ(1,3)+circ(1,2)+circ(1,1)],[circ(2,2)+circ(2,1),circ(2,3)+circ(2,2)+circ(2,1)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 60 | plot([circ(1,3)+circ(1,2)+circ(1,1),circ(1,4)+circ(1,3)+circ(1,2)+circ(1,1)],[circ(2,3)+circ(2,2)+circ(2,1),circ(2,4)+circ(2,3)+circ(2,2)+circ(2,1)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 61 | plot([circ(1,4)+circ(1,3)+circ(1,2)+circ(1,1),0.6],[circ(2,4)+circ(2,3)+circ(2,2)+circ(2,1),circ(2,4)+circ(2,3)+circ(2,2)+circ(2,1)],':bo','MarkerSize',6,'MarkerFaceColor','b','LineWidth',1); 62 | hold off; 63 | axis equal; 64 | xlim([-0.6,0.6]); ylim([-0.6,0.6]); 65 | 66 | %Plot signal 67 | subplot('Position',[0.24,0.85,0.74,0.14]); 68 | plot(t,sig,'-b','LineWidth',1.5); 69 | axis tight; 70 | ylim([-0.6,0.6]); 71 | set(gca,'XDir','reverse'); 72 | xlabel('Time (s)'); 73 | 74 | %Plot complex plane k=1 75 | subplot('Position',[0.04,0.65,0.14,0.14]); 76 | hold on; 77 | plot(c(1)*cos(ang),c(1)*sin(ang),'k','LineWidth',1.5); 78 | plot([0,circ(1,1)],[0,circ(2,1)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 79 | plot([circ(1,1),0.6],[circ(2,1),circ(2,1)],':bo','MarkerSize',6,'MarkerFaceColor','b','LineWidth',1); 80 | hold off; 81 | axis equal; 82 | xlim([-0.6,0.6]); ylim([-0.6,0.6]); 83 | 84 | %Plot signal k=1 85 | subplot('Position',[0.24,0.65,0.74,0.14]); 86 | plot(t,c(1)*sin(phi(:,1)),'-b','LineWidth',1.5); 87 | axis tight; 88 | ylim([-0.6,0.6]); 89 | set(gca,'XDir','reverse'); 90 | xlabel('Time (s)'); 91 | 92 | %Plot complex plane k=2 93 | subplot('Position',[0.04,0.45,0.14,0.14]); 94 | hold on; 95 | plot(c(2)*cos(ang),c(2)*sin(ang),'k','LineWidth',1.5); 96 | plot([0,circ(1,2)],[0,circ(2,2)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 97 | plot([circ(1,2),0.6],[circ(2,2),circ(2,2)],':bo','MarkerSize',6,'MarkerFaceColor','b','LineWidth',1); 98 | hold off; 99 | axis equal; 100 | xlim([-0.6,0.6]); ylim([-0.6,0.6]); 101 | 102 | %Plot signal k=2 103 | subplot('Position',[0.24,0.45,0.74,0.14]); 104 | plot(t,c(2)*sin(phi(:,2)),'-b','LineWidth',1.5); 105 | axis tight; 106 | ylim([-0.6,0.6]); 107 | set(gca,'XDir','reverse'); 108 | xlabel('Time (s)'); 109 | 110 | %Plot complex plane k=3 111 | subplot('Position',[0.04,0.25,0.14,0.14]); 112 | hold on; 113 | plot(c(3)*cos(ang),c(3)*sin(ang),'k','LineWidth',1.5); 114 | plot([0,circ(1,3)],[0,circ(2,3)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 115 | plot([circ(1,3),0.6],[circ(2,3),circ(2,3)],':bo','MarkerSize',6,'MarkerFaceColor','b','LineWidth',1); 116 | hold off; 117 | axis equal; 118 | xlim([-0.6,0.6]); ylim([-0.6,0.6]); 119 | 120 | %Plot signal k=3 121 | subplot('Position',[0.24,0.25,0.74,0.14]); 122 | plot(t,c(3)*sin(phi(:,3)),'-b','LineWidth',1.5); 123 | axis tight; 124 | ylim([-0.6,0.6]); 125 | set(gca,'XDir','reverse'); 126 | xlabel('Time (s)'); 127 | 128 | %Plot complex plane k=4 129 | subplot('Position',[0.04,0.05,0.14,0.14]); 130 | hold on; 131 | plot(c(4)*cos(ang),c(4)*sin(ang),'k','LineWidth',1.5); 132 | plot([0,circ(1,4)],[0,circ(2,4)],'-bo','MarkerSize',4,'MarkerFaceColor','b'); 133 | plot([circ(1,4),0.6],[circ(2,4),circ(2,4)],':bo','MarkerSize',6,'MarkerFaceColor','b','LineWidth',1); 134 | hold off; 135 | axis equal; 136 | xlim([-0.6,0.6]); ylim([-0.6,0.6]); 137 | 138 | %Plot signal k=4 139 | subplot('Position',[0.24,0.05,0.74,0.14]); 140 | plot(t,c(4)*sin(phi(:,4)),'-b','LineWidth',1.5); 141 | axis tight; 142 | ylim([-0.6,0.6]); 143 | set(gca,'XDir','reverse'); 144 | xlabel('Time (s)'); 145 | 146 | drawnow; 147 | 148 | F = getframe(fig(1)); 149 | writeVideo(v,F); 150 | end 151 | 152 | close(v); -------------------------------------------------------------------------------- /ft.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sh01k/teaching/38ab698a19ab22226c9e1423aedae70aea7050fb/ft.mp4 -------------------------------------------------------------------------------- /lin_predict.m: -------------------------------------------------------------------------------- 1 | %% Linear prediction 2 | 3 | %Order 4 | L = 11; 5 | 6 | %Frame 7 | N = 1024; %length 8 | N0 = 4096; %start 9 | 10 | %Sampling freq 11 | fs = 8000; 12 | 13 | %Frequency 14 | f = (0:N/2-1)*fs/N; 15 | 16 | %Hamming window 17 | win_hamm = (0.54-0.46*cos(2*pi*(0:N-1)/(N-1)))'; 18 | 19 | %Read audio file 20 | [data, fs_org] = audioread('path_to_audio_file.wav'); 21 | data_ds = resample(data,fs,fs_org); %downsample 22 | x = data_ds(N0+1:N0+N); %truncation 23 | 24 | %FFT 25 | X = fft(x.*win_hamm,N); 26 | 27 | %Autocorrelation 28 | R = real(ifft(abs(fft(x.*win_hamm,N*2)).^2, N*2)); 29 | R = R(1:N)/N; 30 | 31 | %LP coefficient estimation 32 | Rmat = toeplitz(R(1:L)); 33 | h = Rmat\R(2:L+1); 34 | 35 | res = zeros(N,1); %residual 36 | J = 0; %mean square error 37 | for ii=L+1:N 38 | res(ii) = x(ii)-h'*x(ii-1:-1:ii-L); 39 | J = J + (x(ii)-h'*x(ii-1:-1:ii-L))^2; 40 | end 41 | J = J/(N-L); 42 | 43 | %Power spectrum density 44 | psd = J./(abs(fft([1; -h], N)).^2); 45 | 46 | %Draw figures 47 | figure(1); 48 | plot((1:N)/fs, x, (1:N)/fs, res); 49 | axis tight; 50 | xlabel('Time [s]'); 51 | legend('Signal','Error'); 52 | 53 | figure(2); 54 | plot(f,10*log10(abs(X(1:N/2)).^2/N),f,10*log10(psd(1:N/2))); 55 | axis tight; 56 | xlabel('Frequency [Hz]'); 57 | legend('FFT spectrum','LP spectrum'); 58 | 59 | -------------------------------------------------------------------------------- /markov_proc_o1.m: -------------------------------------------------------------------------------- 1 | %% 1st order Markov process 2 | 3 | T = 512;% %Duration 4 | w = 2*pi*(0:(T/2-1))'/T; %Frequency 5 | 6 | rho = 0.9; %AR coefficient 7 | n = randn(T,1); %Random noise 8 | 9 | %Generate signal 10 | r = zeros(T,1); 11 | for tt=1:T-1 12 | r(tt+1) = r(tt)*rho + n(tt); 13 | end 14 | 15 | %Hamming window 16 | win_hamm = (0.54-0.46*cos(2*pi*(0:T-1)/(T-1)))'; 17 | 18 | %Autocorrelation 19 | cn = real(ifft(abs(fft(n.*win_hamm,T*2)).^2, T*2)); 20 | cn = cn(1:T)/cn(1); 21 | cr = real(ifft(abs(fft(r.*win_hamm,T*2)).^2, T*2)); 22 | cr = cr(1:T)/cr(1); 23 | 24 | a = 1-rho; 25 | cr_i = exp(-a.*(0:T-1)); 26 | 27 | %FFT 28 | Sn = abs(fft(n.*win_hamm,T)).^2/T; 29 | Sr = abs(fft(r.*win_hamm,T)).^2/T; 30 | 31 | Sr_i = 2*a./(a^2+w.^2); 32 | 33 | %Draw figures 34 | figure(1); 35 | plot(1:T,n,1:T,r); 36 | axis tight; 37 | xlabel('t'); 38 | legend('n(t)','r(t)'); 39 | 40 | figure(2); 41 | plot(1:T,cn,1:T,cr,1:T,cr_i); 42 | axis tight; 43 | xlim([1,64]); 44 | xlabel('\tau'); 45 | legend('C_n(\tau)','C_r(\tau)','C_{r,i}(\tau)'); 46 | 47 | figure(3); 48 | plot(w,10*log10(Sn(1:T/2)),w,10*log10(Sr(1:T/2)),w,10*log10(Sr_i)); 49 | axis tight; 50 | xlabel('\omega'); 51 | legend('S_n(\omega)','S_r(\omega)','S_{r,i}(\omega)'); 52 | 53 | -------------------------------------------------------------------------------- /neural_net.m: -------------------------------------------------------------------------------- 1 | %% Training 2 | %download from http://yann.lecun.com/exdb/mnist/ 3 | train_image_file = 'train-images-idx3-ubyte'; 4 | train_label_file = 'train-labels-idx1-ubyte'; 5 | test_image_file = 't10k-images-idx3-ubyte'; 6 | test_label_file = 't10k-labels-idx1-ubyte'; 7 | 8 | %load data 9 | images = load_mnist_image(train_image_file, 1, 1); 10 | labels = load_mnist_label(train_label_file, 1); 11 | 12 | %define weights 13 | W1 = randn(50,784); W2 = randn(10,50); 14 | b1 = randn(50,1); b2 = randn(10,1); 15 | 16 | %learning rate 17 | eps = 0.01; 18 | max_itr = 100; 19 | 20 | batch_size = 100; 21 | 22 | for jj = 1:max_itr 23 | E = 0; 24 | for ii = 1:size(images,2)/batch_size 25 | %prediction 26 | x = images(:,(ii-1)*batch_size+1:ii*batch_size); 27 | u1 = W1*x+b1; 28 | z1 = sigmoid(u1); 29 | u2 = W2*z1+b2; 30 | y = softmax(u2); 31 | 32 | d = labels(:,(ii-1)*batch_size+1:ii*batch_size); 33 | E = E + sum(cross_entropy_error(y,d)); 34 | 35 | %backpropagation 36 | delta2 = y-d; 37 | delta1 = (W2'*delta2).*sigmoid_grad(u1); 38 | grad2 = delta2*[z1; ones(1,batch_size)]'; 39 | grad1 = delta1*[x; ones(1,batch_size)]'; 40 | 41 | %update weights 42 | W2 = W2 - eps*grad2(:,1:(size(grad2,2)-1)); 43 | b2 = b2 - eps*grad2(:,size(grad2,2)); 44 | W1 = W1 - eps*grad1(:,1:(size(grad1,2)-1)); 45 | b1 = b1 - eps*grad1(:,size(grad1,2)); 46 | end 47 | E = E/size(images,2); 48 | fprintf('itr: %d, cost: %f\n',jj,E); 49 | end 50 | 51 | %% Test 52 | %load data 53 | images_test = load_mnist_image(test_image_file, 1, 1); 54 | labels_test = load_mnist_label(test_label_file, 0); 55 | 56 | accuracy_cnt = 0; 57 | for ii = 1:size(images_test,2)/batch_size 58 | %prediction 59 | x = images_test(:,(ii-1)*batch_size+1:ii*batch_size); 60 | u1 = W1*x+b1; 61 | z1 = sigmoid(u1); 62 | u2 = W2*z1+b2; 63 | y = softmax(u2); 64 | 65 | [~,idx] = max(y,[],1); 66 | d = labels_test((ii-1)*batch_size+1:ii*batch_size)'+1; 67 | accuracy_cnt = accuracy_cnt + sum(d==idx); 68 | end 69 | accuracy_rate = accuracy_cnt/size(images_test,2)*100; 70 | 71 | fprintf('Accuracy rate: %f%%\n',accuracy_rate); 72 | 73 | %% Functions 74 | function [images,numImages,numRows,numCols] = load_mnist_image(fname, normalize, flatten) 75 | fp = fopen(fname, 'rb'); 76 | 77 | magic = fread(fp, 1, 'int32', 0, 'b'); 78 | if magic ~= 2051; fprintf('Incorrect magic number\n'); return; end 79 | 80 | numImages = fread(fp, 1, 'int32', 0, 'b'); 81 | numRows = fread(fp, 1, 'int32', 0, 'b'); 82 | numCols = fread(fp, 1, 'int32', 0, 'b'); 83 | 84 | images = fread(fp, inf, 'uchar'); 85 | images = reshape(images, numCols, numRows, numImages); 86 | images = permute(images,[2 1 3]); 87 | 88 | fclose(fp); 89 | 90 | if flatten 91 | images = reshape(images, size(images, 1) * size(images, 2), size(images, 3)); 92 | end 93 | 94 | if normalize 95 | images = double(images) / 255; 96 | end 97 | 98 | end 99 | 100 | function [labels,numLabels] = load_mnist_label(fname, one_hot_flg) 101 | fp = fopen(fname, 'rb'); 102 | 103 | magic = fread(fp, 1, 'int32', 0, 'b'); 104 | if magic ~= 2049; fprintf('Incorrect magic number\n'); return; end 105 | 106 | numLabels = fread(fp, 1, 'int32', 0, 'b'); 107 | 108 | labels = fread(fp, inf, 'uchar'); 109 | 110 | fclose(fp); 111 | 112 | if one_hot_flg 113 | labels_vec = zeros(10,numLabels); 114 | for ii=1:numLabels 115 | labels_vec(labels(ii)+1,ii) = 1; 116 | end 117 | labels = labels_vec; 118 | end 119 | 120 | end 121 | 122 | function y = sigmoid(x) 123 | y = 1.0 ./ (1.0 + exp(-x)); 124 | end 125 | 126 | function y = sigmoid_grad(x) 127 | y = (1.0 - sigmoid(x)) .* sigmoid(x); 128 | end 129 | 130 | function y = softmax(x) 131 | c = max(x,[],1); 132 | y = exp(x-c)./sum(exp(x-c)); 133 | end 134 | 135 | function E = cross_entropy_error(y,d) 136 | E = -sum(d.*log(y+1e-7),1); 137 | end 138 | -------------------------------------------------------------------------------- /plane_wave.m: -------------------------------------------------------------------------------- 1 | %% Plane wave function 2 | 3 | c = 340; %sound speed [m/s] 4 | freq = 1000; %frequency [Hz] 5 | k = 2*pi*freq/c; %wave number [rad/m] 6 | 7 | %length [m] 8 | Lx = 1.2; 9 | Lz = 1.0; 10 | 11 | %interval [m] 12 | dx = 0.01; 13 | dz = 0.01; 14 | 15 | %number of points 16 | Nx = ceil(Lx/dx); 17 | Nz = ceil(Lz/dz); 18 | 19 | x = ((0:Nx-1)-Nx/2)'*dx; 20 | y = 0; 21 | z = (0:Nz-1)'*dz; 22 | 23 | x_vec = reshape(x*ones(1,Nz),1,Nx*Nz); 24 | y_vec = reshape(y*ones(Nx,Nz),1,Nx*Nz); 25 | z_vec = reshape((z*ones(1,Nx))',1,Nx*Nz); 26 | 27 | %direction 28 | theta = pi/3; 29 | phi = 0; 30 | 31 | %wave number 32 | kx = k*cos(phi)*sin(theta); 33 | ky = k*sin(phi)*sin(theta); 34 | kz = sqrt(k^2-kx^2-ky^2); 35 | k_vec = [kx, ky, kz]'; 36 | 37 | %pressure 38 | p_pw = exp(1i*k_vec'*[x_vec;y_vec;z_vec]); 39 | p_pw = reshape(p_pw,Nx,Nz); 40 | 41 | %draw figures 42 | figure(1); 43 | imagesc([min(x),max(x)],[min(z),max(z)],real(p_pw).'); 44 | set(gca,'YDir','normal'); 45 | axis equal; 46 | axis tight; 47 | caxis([-1,1]); 48 | colormap(flipud(pink)); 49 | colorbar; 50 | xlabel('x (m)'); ylabel('z (m)'); 51 | -------------------------------------------------------------------------------- /sph_harm.m: -------------------------------------------------------------------------------- 1 | %number of angles 2 | ang_num = 64; 3 | 4 | %angle [rad] 5 | theta = ((0:ang_num-1)*pi/ang_num)'; 6 | phi = ((0:ang_num-1)*2*pi/ang_num)'; 7 | 8 | theta_mat = theta*ones(1,ang_num); 9 | phi_mat = (phi*ones(1,ang_num))'; 10 | 11 | theta_vec = reshape(theta_mat,ang_num*ang_num,1); 12 | phi_vec = reshape(phi_mat,ang_num*ang_num,1); 13 | 14 | %order and degree 15 | n=3; 16 | m=-2; 17 | 18 | %associated Legendre function 19 | Pnm = legendre(n,cos(theta_vec)); 20 | 21 | %normalization coefficient 22 | n_coef = ((-1).^min(m,0)).*sqrt(((2*n+1)/(4*pi))*(factorial(n-abs(m))/factorial(n+abs(m)))); 23 | 24 | %spherical harmonic function 25 | Ynm_vec = n_coef.*(Pnm(abs(m)+1,:).').*exp(1i*m*phi_vec); 26 | Ynm = reshape(Ynm_vec,[ang_num,ang_num]); 27 | 28 | %draw figures 29 | XX = cos(phi_mat).*sin(theta_mat); 30 | YY = sin(phi_mat).*sin(theta_mat); 31 | ZZ = cos(theta_mat); 32 | 33 | XX = [XX, XX(:,1); XX(1,:) XX(ang_num,ang_num)]; 34 | YY = [YY, YY(:,1); YY(1,:) YY(ang_num,ang_num)]; 35 | ZZ = [ZZ, ZZ(:,1); ZZ(1,:) ZZ(ang_num,ang_num)]; 36 | 37 | figure(1); 38 | surf(XX,YY,ZZ,real(Ynm)); 39 | axis equal; 40 | colormap(flipud(pink)); 41 | % colorbar; 42 | xlabel('x (m)'); ylabel('y (m)'); zlabel('z (m)'); 43 | 44 | Ynm_plt = abs(real([Ynm, Ynm(:,1); Ynm(1,:), Ynm(ang_num,ang_num)])); 45 | 46 | figure(2); 47 | surf(Ynm_plt.*XX,Ynm_plt.*YY,Ynm_plt.*ZZ,real(Ynm)); 48 | axis equal; 49 | colormap(flipud(pink)); 50 | % colorbar; 51 | xlabel('x (m)'); ylabel('y (m)'); zlabel('z (m)'); -------------------------------------------------------------------------------- /wiener_filter.m: -------------------------------------------------------------------------------- 1 | %% Wiener filtering for speech enhancement 2 | 3 | %Sampling freq 4 | fs = 8000; 5 | 6 | %Frame 7 | fftlen = 256; 8 | fftshift = fftlen/2; 9 | 10 | %Frequency 11 | f = (0:fftlen/2-1)*fs/fftlen; 12 | 13 | %Squre root of Hann window 14 | win_sqrthann = sqrt(0.5-0.5*cos(2*pi*(0:fftlen-1)/(fftlen-1)))'; 15 | 16 | %Read audio file 17 | [data, fs_org] = audioread('path_to_audio_file.wav'); 18 | s = resample(data,fs,fs_org); %downsample 19 | len = length(s); 20 | 21 | snr = 10; 22 | Ps_i = sum(abs(s).^2)/len; 23 | Pn_i = Ps_i/(10^(snr/10)); 24 | 25 | %Generate noise 26 | n = sqrt(Pn_i)*randn(len,1); 27 | 28 | %Observation 29 | x = s + n; 30 | 31 | %Estimate noise amplitude from first several frames 32 | nFrame_noise = 10; 33 | Sn = zeros(fftlen,1); 34 | for tt=1:nFrame_noise 35 | Sn = Sn + abs(fft(x((tt-1)*fftshift+1:(tt-1)*fftshift+fftlen).*win_sqrthann,fftlen)); 36 | end 37 | Sn = Sn/nFrame_noise; 38 | 39 | %Filtering 40 | s_est = zeros(len,1); 41 | for tt=1:floor((len-fftlen)/fftshift) 42 | x_t = x((tt-1)*fftshift+1:(tt-1)*fftshift+fftlen); 43 | X_t = fft(x_t.*win_sqrthann,fftlen); 44 | Sx = abs(X_t); 45 | 46 | %Spectral subtraction to estimate speech psd 47 | Ss = max(Sx - Sn, 0.0); 48 | 49 | Pn = Sn.^2; 50 | Ps = Ss.^2; 51 | WF = Ps./(Pn+Ps); 52 | 53 | S_t = X_t.*WF; 54 | s_t = real(ifft(S_t,fftlen)).*win_sqrthann; 55 | s_est((tt-1)*fftshift+1:(tt-1)*fftshift+fftlen) = s_est((tt-1)*fftshift+1:(tt-1)*fftshift+fftlen) + s_t; 56 | end 57 | 58 | figure(1); 59 | plot(1:len,x); 60 | xlim([1,len]); ylim([-0.5,0.5]); 61 | grid on; 62 | 63 | figure(2); 64 | plot(1:len,s); 65 | xlim([1,len]); ylim([-0.5,0.5]); 66 | grid on; 67 | 68 | figure(3); 69 | plot(1:length(s_est),s_est); 70 | xlim([1,len]); ylim([-0.5,0.5]); 71 | grid on; 72 | --------------------------------------------------------------------------------