Advertisement
Guest User

Sensor-filer

a guest
Jan 28th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. %fil 1 = doppler
  2.  
  3. meanTp = round(mean(diff(rawTimes)))*1e-6;
  4. fs = 1/meanTp(1); %samples per second
  5. dt = 1/fs; % seconds per sample
  6. from = 1000;
  7. to = 30000;
  8. Nfft = 2^(14);
  9. N = to - from; %samples; % samples taken
  10.  
  11. RadarIF_I = allData.RadarIF_I -512;
  12.  
  13. radar_fft = fft(RadarIF_I(from:to),Nfft);
  14. f = 1/(Nfft*dt)*(-Nfft/2:1:Nfft/2-1);
  15.  
  16. radar_fft(1) = 0;
  17. [a, b] = max(abs(radar_fft));
  18. peak = f(Nfft/2 + b);
  19.  
  20. vel = peak/160.9;
  21.  
  22. s_fft = fftshift(radar_fft);
  23. s_fft = s_fft/max(s_fft);
  24. figure;
  25. plot(f, 20*log10(abs(s_fft)), 'r-');
  26. title(['Magnitude with peak at: ' num2str(peak) ' Velocity: ' num2str(vel)]);
  27. axis([-2000 2000 -70 10]);
  28. xlabel('Frequency');
  29. ylabel('magnitude');
  30. grid on;
  31.  
  32.  
  33. %fil 2 = radfreq
  34.  
  35. meanTp = round(mean(diff(rawTimes)))*1e-6;
  36. fs = 1/meanTp(1); %samples per second
  37. dt = 1/fs; % seconds per sample
  38. N = 5000; %samples; % samples taken
  39.  
  40. Radar_I = allData.RadarIF_I - 512;
  41. Radar_Q = allData.RadarIF_Q - 512;
  42. %ffts
  43. %RadarIF_I_fft = fftshift(fft(Radar_I(5000:12000),N));
  44. %RadarIF_Q_fft = fftshift(fft(Radar_Q(5000:12000), N));
  45. RadarIF_I_fft = fft(Radar_I(8000:13000),N);
  46. RadarIF_I_fft = RadarIF_I_fft(1:N/2);
  47.  
  48. RadarIF_Q_fft = fft(Radar_Q(8000:13000), N);
  49. RadarIF_Q_fft = RadarIF_Q_fft(1:N/2);
  50.  
  51. %Freq specs
  52. dF = fs/N;
  53. %f = -fs/2:dF:fs/2-dF;
  54. f = (0:N/2-1)*fs/N;
  55.  
  56. %spectrum plot
  57. figure;
  58. plot(f, abs(RadarIF_I_fft), '-o',...
  59. f, abs(RadarIF_Q_fft), '-o'...
  60. );
  61. xlabel('Frequency (in Hertz)');
  62. title('Magnitude response');
  63.  
  64. %fil 3 = raspianalyze
  65.  
  66. %This script will import the binary data from files written by the C
  67. % program on Raspberry Pi.
  68. %
  69. % The script uses the function raspiImport to do the actual import and
  70. % conversion from binary data to numerical values. Make sure you have
  71. % downloaded it as well.
  72. %
  73. % After the import function is finished, the data are written to the
  74. % variable rawData. The number of samples is returned in a variable samples
  75.  
  76. % Definitions
  77. channels = 5; % Number of ADC channels used
  78.  
  79. % Open, import and close binary data file produced by Raspberry Pi
  80. %% FIXME: Change this.
  81. path = 'W:\adcData.bin';
  82.  
  83.  
  84. % Run function to import all data from the binary file. If you change the
  85. % name or want to read more files, you must change the function
  86. % accordingly.
  87. [rawData, nomTp] = raspiImport(path,channels);
  88.  
  89. % Plot all raw data and corresponding amplitude response
  90. fh_raw = figure; % fig handle
  91. plot(rawData,'-o');
  92. ylim([0, 4095]) % 12 bit ADC gives values in range [0, 4095]
  93. xlabel('sample');
  94. ylabel('conversion value');
  95. legendStr = cell(1,channels);
  96. for i = 1:channels
  97. legendStr{1,i} = ['ch. ' num2str(i)];
  98. end
  99. legend(legendStr,'location','best');
  100. title('Raw ADC data');
  101.  
  102. %fil 4 = raspiimport
  103.  
  104. % raspiImport takes in a path string and imports a specified binary data file
  105. % from this path. It returns the raw data as a matrix NUM_SAMPLES x
  106. % NUM_CAHNNELS, and the sample period in seconds.
  107. function [rawData, sample_period] = raspiImport(path,channels)
  108.  
  109. % Input argument handling
  110. if nargin < 2
  111. channels = 1; % Assume single channel as nothing was specified
  112. warning('Number of channels not defined, assuming single...');
  113. end
  114.  
  115. % Open file
  116. fidAdcData = fopen(path);
  117. % Read binary data to local variables
  118. sample_period = fread(fidAdcData, 1, 'double')*1.0e-06;
  119. adcData = fread(fidAdcData,'uint16');
  120. % Close files properly after import
  121. fclose(fidAdcData);
  122.  
  123. lenAdcData = length(adcData); % Total number of ADC data bytes
  124. samples = lenAdcData/channels; % Total number of samples
  125.  
  126. %reshape one-dimensional data array into NUM_SAMPLES x NUM_CHANNELS data matrix
  127. rawData = reshape(adcData, channels, samples)';
  128.  
  129. end
  130.  
  131. %fil 5 = test_mic
  132.  
  133.  
  134. fs = 16000;
  135. r = 16;
  136. %insert samplerange you want to crosscorrelate
  137. p = 1:500000;
  138.  
  139.  
  140.  
  141. k = rawData(:,1)-2048;
  142. k = interp(k,r);
  143. l = rawData(:,3)-2048;
  144. l = interp(l,r);
  145. m = rawData(:,4)-2048;
  146. m = interp(m,r);
  147.  
  148. %Removing DC components BEFORE crosscorrelation
  149. m1 = k;
  150. m2 = l;
  151. m3 = m;
  152.  
  153. %Crosscorrealtion between all three mics
  154. [acor1, lag1] = xcorr(m1, m2);
  155. [~, I_1] = max( abs(acor1));
  156. lagDiff1 = lag1(I_1);
  157. timeDiff1 = lagDiff1/(fs);
  158.  
  159. [acor2, lag2] = xcorr(m1, m3);
  160. [~, I2] = max( abs(acor2));
  161. lagDiff2 = lag2(I2);
  162. timeDiff2 = lagDiff2/(fs);
  163.  
  164. [acor3, lag3] = xcorr(m2, m3);
  165. [~, I3] = max( abs(acor3));
  166. lagDiff3 = lag3(I3);
  167. timeDiff3 = lagDiff3/(fs);
  168.  
  169. %Incoming angle estimation
  170. theta = atan(sqrt(3)*((timeDiff1 + timeDiff2)/(timeDiff1 - timeDiff2 - 2*timeDiff3)));
  171. if (lagDiff3 < 0),
  172. theta = theta + pi;
  173. elseif (lagDiff1 == lagDiff2 && lagDiff1 > 0),
  174. theta = -pi/2;
  175. elseif (lagDiff1 == lagDiff2 && lagDiff1 < 0),
  176. theta = pi/2;
  177. end
  178.  
  179. %plot relevant figures
  180. figure;
  181. subplot(3,1,1);
  182. plot(lag1/fs, acor1);
  183. title('Lag from m2 to m1');
  184. xlabel(['sample difference: ' num2str(lagDiff1) ' Time difference: ' num2str(timeDiff1)]);
  185.  
  186. subplot(3,1,2);
  187. plot(lag2/fs, acor2);
  188. title('Lag from m3 to m1');
  189. xlabel(['sample difference: ' num2str(lagDiff2) ' Time difference: ' num2str(timeDiff2)]);
  190.  
  191. subplot(3,1,3);
  192. plot(lag3/fs, acor3);
  193. title('Lag from m3 to m2');
  194. xlabel(['sample difference: ' num2str(lagDiff3) ' Time difference: ' num2str(timeDiff3)]);
  195.  
  196. figure;
  197. plot(p,m1, '-o',...
  198. p,m2, '-o',...
  199. p,m3, '-o'...
  200. );
  201. title('Part of signals m1, m2 and m3 that are crosscorrelated');
  202. xlabel('t [s]');
  203. ylabel('12-bit Conversion value');
  204. legend('Mic1','Mic2','Mic3');
  205.  
  206.  
  207. figure;
  208.  
  209. plot([0 sqrt(3)/2],[1 -0.5], 'g--o',...
  210. [-sqrt(3)/2 0], [-0.5 1], 'b--o',...
  211. [sqrt(3)/2 -sqrt(3)/2], [-0.5 -0.5], 'r--o',...
  212. [0 10*cos(theta)], [0 10*sin(theta)]...
  213. );
  214. axis([-1.5 1.5 -1.5 1.5]);
  215. text(0, 1.2, 'm1');
  216. text(-1, -0.7, 'm2');
  217. text(1, -0.7, 'm3');
  218. grid on;
  219.  
  220. %fil 6 = komm
  221.  
  222. load('project1-data.mat');
  223. Fs = 1/(t(4)-t(3));
  224. lengde = length(x);
  225. %power
  226. power = bandpower(x,Fs,[0 Fs/2]);
  227. rRMS = rms(x)^2;
  228.  
  229. %power density
  230. xdft = fft(x);
  231. xdft = xdft(1:lengde/2+1);
  232. psdx = (1/(Fs*lengde)) * abs(xdft).^2;
  233. psdx(2:end-1) = 2*psdx(2:end-1);
  234. freq = 0:Fs/length(x):Fs/2;
  235.  
  236. figure()
  237. plot(freq,10*log10(psdx))
  238. grid on
  239. title('Periodogram Using FFT')
  240. xlabel('Frequency (Hz)')
  241. ylabel('Power/Frequency (dB/Hz)')
  242.  
  243. figure()
  244. periodogram(x,rectwin(length(x)),length(x),Fs)
  245.  
  246. %autocorrelation
  247. figure()
  248. autocorr(x);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement