Advertisement
Guest User

Micdelay

a guest
Jan 28th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. meanTp = round(mean(diff(rawTimes)))*1e-6;
  2. fs = 1/meanTp(1);
  3. %insert samplerange you want to crosscorrelate
  4. a = 2000;
  5. b = 20000;
  6.  
  7. %Removing DC components BEFORE crosscorrelation
  8. m1 = allData.Mic1(a:b) -512;
  9. m2 = allData.Mic2(a:b) -512;
  10. m3 = allData.Mic3(a:b) -512;
  11.  
  12. %Crosscorrealtion between all three mics
  13. [acor1, lag1] = xcorr(m1, m2);
  14. [~, I_1] = max( abs(acor1));
  15. lagDiff1 = lag1(I_1);
  16. timeDiff1 = lagDiff1/fs;
  17.  
  18. [acor2, lag2] = xcorr(m1, m3);
  19. [~, I2] = max( abs(acor2));
  20. lagDiff2 = lag2(I2);
  21. timeDiff2 = lagDiff2/fs;
  22.  
  23. [acor3, lag3] = xcorr(m2, m3);
  24. [~, I3] = max( abs(acor3));
  25. lagDiff3 = lag3(I3);
  26. timeDiff3 = lagDiff3/fs;
  27.  
  28. %Incoming angle estimation
  29. theta = atan(sqrt(3)*((timeDiff1 + timeDiff2)/(timeDiff1 - timeDiff2 - 2*timeDiff3)));
  30. if (lagDiff3 < 0),
  31. theta = theta + pi;
  32. elseif (lagDiff1 == lagDiff2 && lagDiff1 > 0),
  33. theta = -pi/2;
  34. elseif (lagDiff1 == lagDiff2 && lagDiff1 < 0),
  35. theta = pi/2;
  36. end
  37.  
  38. %plot relevant figures
  39. figure;
  40. subplot(3,1,1);
  41. plot(lag1/fs, acor1);
  42. title('Lag from m2 to m1');
  43. xlabel(['sample difference: ' num2str(lagDiff1) ' Time difference: ' num2str(timeDiff1)]);
  44.  
  45. subplot(3,1,2);
  46. plot(lag2/fs, acor2);
  47. title('Lag from m3 to m1');
  48. xlabel(['sample difference: ' num2str(lagDiff2) ' Time difference: ' num2str(timeDiff2)]);
  49.  
  50. subplot(3,1,3);
  51. plot(lag3/fs, acor3);
  52. title('Lag from m3 to m2');
  53. xlabel(['sample difference: ' num2str(lagDiff3) ' Time difference: ' num2str(timeDiff3)]);
  54.  
  55. figure;
  56. plot(allTimes.Mic1(a:b),m1, '-o',...
  57. allTimes.Mic2(a:b),m2, '-o',...
  58. allTimes.Mic3(a:b),m3, '-o'...
  59. );
  60. title('Part of signals m1, m2 and m3 that are crosscorrelated');
  61. xlabel('t [s]');
  62. ylabel('10-bit Conversion value');
  63. legend('Mic1','Mic2','Mic3');
  64.  
  65. figure;
  66.  
  67. plot([0 sqrt(3)/2],[1 -0.5], 'g--o',...
  68. [-sqrt(3)/2 0], [-0.5 1], 'b--o',...
  69. [sqrt(3)/2 -sqrt(3)/2], [-0.5 -0.5], 'r--o',...
  70. [0 10*cos(theta)], [0 10*sin(theta)]...
  71. );
  72. axis([-1.5 1.5 -1.5 1.5]);
  73. text(0, 1.2, 'm1');
  74. text(-1, -0.7, 'm2');
  75. text(1, -0.7, 'm3');
  76. grid on;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement