Skip to content

Commit bd33127

Browse files
author
gaoyongyu
committed
first
0 parents  commit bd33127

6 files changed

+104
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Computation of two signal convolution and correlation
3+
==============================
4+
This repo is self-coded for two signal convolution and correlation.
5+
6+
My objective to do that is to review the basic implementation of how signal works..
7+
8+
How to use
9+
==========
10+
Check __main.m__

cross_correlation.m

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
clear all;clc;close all;
2+
farend_speech_path = '/home/yongyug/data/aec_challenge/datasets/synthetic/farend_speech/farend_speech_fileid_8109.wav';
3+
nearend_mic_path = '/home/yongyug/data/aec_challenge/datasets/synthetic/nearend_mic_signal/nearend_mic_fileid_8109.wav';
4+
5+
[farend_speech_data, fs] = audioread(farend_speech_path);
6+
[nearend_mic_data, fs] = audioread(nearend_mic_path);
7+
8+
e_idx = min(size(farend_speech_data, 1), size(nearend_mic_data, 1));
9+
[cofs, lags] = xcorr(farend_speech_data, nearend_mic_data, 'coeff');
10+
[val, idx] = max(cofs);
11+
offset = lags(idx);
12+
minlength = length(farend_speech_data) - abs(offset);
13+
disp(offset);
14+
plot(lags,cofs);
15+
xlabel('shift');ylabel('relative cof');
16+
17+
if offset>0
18+
farend_speech_data = [farend_speech_data(offset:160000); zeros(offset, 1)];
19+
nearend_mic_data = nearend_mic_data(1:160000);
20+
else
21+
nearend_mic_data = [nearend_mic_data(-offset:160000) ;zeros(-offset, 1)];
22+
farend_speech_data = farend_speech_data(1:160000);
23+
end
24+
25+
audiowrite('/home/yongyug/data/8109_far.wav', farend_speech_data, 16000);
26+
audiowrite('/home/yongyug/data/8109_mic.wav', nearend_mic_data, 16000);

learn_cross_cor.m

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
clear all;clc;close all;
2+
dt = .1;
3+
t = [0:dt:100];
4+
x = cos(t);
5+
plot(x);

main.m

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
clear all;clc;close all;
2+
3+
%%%% you can set arry a and arry b in different value, try some trails
4+
a = [1 2 1 2 1 ];
5+
b = [2 2 3 3 3];
6+
7+
%compute convolution
8+
res = my_conv(a,b);
9+
c = conv(a, b);
10+
d = xcorr(a, b);
11+
e = my_conv_usingfft(a, b);
12+
13+
14+
b = flip(b);
15+
%compute corss correlation
16+
f = my_conv_usingfft(a,b);
17+
g = my_conv(a, b);
18+
19+
%%%
20+
%%% Check the results of my function with official function
21+
%%% Check if c = e = res for convolution computation
22+
%%% Check if d = f = g
23+
%%%
24+
25+
26+
27+

my_conv.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [ convRes ] = my_conv(aSignal, bSignal)
2+
aLen= length(aSignal);
3+
bLen = length(bSignal);
4+
N = aLen + bLen - 1; %lenth of convolution results must be aLen + bLen -1
5+
6+
bSignal = [bSignal zeros(1, aLen - 1)];
7+
bSignal = flip(bSignal);%flip the signal
8+
9+
aBuffer = [aSignal zeros(1, aLen - 1)];
10+
bBuffer = zeros(1, length(aBuffer));
11+
convRes = zeros(1, N);
12+
13+
for i = 1: N
14+
15+
bBuffer(:, 2:length(bBuffer)) = bBuffer(:, 1:length(bBuffer)-1); %shift to the right
16+
bBuffer(:, 1) = bSignal(:, N-i+1); %move bsignal from left to right
17+
convRes(:, i) = aBuffer(:,:) * bBuffer(:, :)'; %compute convolution
18+
19+
end
20+
21+
end
22+

my_conv_usingfft.m

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [ c ] = my_conv_usingfft( a ,b )
2+
alen = length(a);
3+
blen = length(b);
4+
5+
a = [a zeros(1, blen-1)];
6+
b = [b zeros(1, alen-1)];
7+
8+
A = fft(a);
9+
B = fft(b);
10+
c = real(ifft(A.*B));
11+
12+
13+
end
14+

0 commit comments

Comments
 (0)