东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1341|回复: 1
打印 上一主题 下一主题

[Python] 一维ca-cfar(均值类恒虚警检测)与二维ca-cfar算法matlab实现

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14439
QQ
跳转到指定楼层
楼主
发表于 2021-11-15 15:24:28 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式





一维ca-cfar(均值类恒虚警检测)与二维ca-cfar算法matlab实现


  1. % Implement 1D CFAR using lagging cells on the given noise and target scenario.

  2. % Close and delete all currently open figures
  3. close all;

  4. % Data_points
  5. Ns = 1000;

  6. % Generate random noise
  7. s=abs(randn(Ns,1));

  8. %Targets location. Assigning bin 100, 200, 300 and 700 as Targets with the amplitudes of 8, 9, 4, 11.
  9. s([100 ,200, 350, 700])=[8 15 7 13];

  10. %plot the output
  11. figure ('Name','original_signal'),plot(s);

  12. % TODO: Apply CFAR to detect the targets by filtering the noise.

  13. % 1. Define the following:
  14. % 1a. Training Cells
  15. T = 12;
  16. % 1b. Guard Cells
  17. G = 4;

  18. % Offset : Adding room above noise threshold for desired SNR
  19. offset=5;

  20. % Vector to hold threshold values
  21. threshold_cfar = [];  % 存储阈值的数组

  22. %Vector to hold final signal after thresholding
  23. signal_cfar = [];     % 是否有目标?

  24. % 2. Slide window across the signal length
  25. for i = 1:(Ns-(G+T+1))     

  26.     % 2. - 5. Determine the noise threshold by measuring it within the training cells
  27.    
  28.     noise_level =sum(s(i:i+T-1));
  29.    
  30.     % 6. Measuring the signal within the CUT
  31.    
  32.     threshold = (noise_level/T)*offset;   % 这里是乘以offset
  33.     threshold_cfar=[threshold_cfar,{threshold}];
  34.    
  35.     signal=s(i+T+G);
  36.    
  37.     % 8. Filter the signal above the threshold
  38.     if (signal<threshold)
  39.         signal=0;   % 不是目标
  40.     end
  41.     signal_cfar = [signal_cfar, {signal}];
  42. end




  43. % plot the filtered signal
  44. % signal_cfar2 = cell2mat(signal_cfar);
  45. figure,plot (cell2mat(signal_cfar),'g--');

  46. % plot original sig, threshold and filtered signal within the same figure.
  47. figure,plot(s);
  48. hold on,plot(cell2mat(circshift(threshold_cfar,G)),'r--','LineWidth',2)
  49. hold on, plot (cell2mat(circshift(signal_cfar,(T+G))),'g--','LineWidth',4);
  50. legend('Ori_Signal','CFAR Threshold','detection')
复制代码




  1. clear all
  2. close all;
  3. clc;

  4. %% Radar Specifications
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % Frequency of operation = 77GHz
  7. % Max Range = 200m
  8. % Range Resolution = 1 m
  9. % Max Velocity = 100 m/s
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%

  11. max_range=200;
  12. c = 3e8;
  13. range_resolution= 1;

  14. %Operating carrier frequency of Radar
  15. fc= 77e9;             %carrier freq
  16. wavelength = c/fc;

  17. %% User Defined Range and Velocity of target
  18. % *%TODO* :
  19. % define the target's initial position and velocity. Note : Velocity
  20. % remains contant
  21. target_pos=100;
  22. target_speed=30;


  23. %% FMCW Waveform Generation

  24. % *%TODO* :
  25. %Design the FMCW waveform by giving the specs of each of its parameters.
  26. % Calculate the Bandwidth (B), Chirp Time (Tchirp) and Slope (slope) of the FMCW
  27. % chirp using the requirements above.

  28. B_sweep = c/(2*range_resolution); %Calculate the Bandwidth (B)

  29. T_chirp = 5.5*2*max_range/c;   % 为何啊?

  30. slope=B_sweep/T_chirp;



  31.                                                          
  32. %The number of chirps in one sequence. Its ideal to have 2^ value for the ease of running the FFT
  33. %for Doppler Estimation.
  34. Nd=128;                   % #of doppler cells OR #of sent periods % number of chirps

  35. %The number of samples on each chirp.
  36. Nr=1024;                  %for length of time OR # of range cells

  37. % Timestamp for running the displacement scenario for every sample on each
  38. % chirp
  39. t=linspace(0,Nd*T_chirp,Nr*Nd); %total time for samples


  40. %Creating the vectors for Tx, Rx and Mix based on the total samples input.
  41. Tx=zeros(1,length(t)); %transmitted signal
  42. Rx=zeros(1,length(t)); %received signal
  43. Mix = zeros(1,length(t)); %beat signal

  44. %Similar vectors for range_covered and time delay.
  45. r_t=zeros(1,length(t));  % 每时每刻的距离
  46. td=zeros(1,length(t));   % 距离算出来的时延


  47. %% Signal generation and Moving Target simulation
  48. % Running the radar scenario over the time.

  49. doppler_resolution = 1 / (Nd * T_chirp);

  50. v_resolution = (wavelength*doppler_resolution)/2;  % 速度分辨率为2.07 为何?

  51. for i=1:length(t)         
  52.    
  53.    
  54.     % *%TODO* :
  55.     %For each time stamp update the Range of the Target for constant velocity.
  56.    
  57.     r_t(i) = target_pos+(target_speed*t(i));
  58.     td(i) = 2*r_t(i)/c; % Time delay
  59.    
  60.     % *%TODO* :
  61.     %For each time sample we need update the transmitted and
  62.     %received signal.
  63.     Tx(i) = cos(2 * pi * (fc * t(i) + slope * (t(i)^2)/2));
  64.     Rx(i) = cos(2 * pi * (fc * (t(i) - td(i)) + slope * ((t(i)-td(i))^2)/2));
  65.    
  66.     % *%TODO* :
  67.     %Now by mixing the Transmit and Receive generate the beat signal
  68.     %This is done by element wise matrix multiplication of Transmit and
  69.     %Receiver Signal
  70.     Mix(i) = Tx(i) .* Rx(i);    % 不是发射信号的复共轭 不是复信号啊
  71.    
  72. end

  73. %% RANGE MEASUREMENT


  74. % *%TODO* :
  75. %reshape the vector into Nr*Nd array. Nr and Nd here would also define the size of
  76. %Range and Doppler FFT respectively.

  77. Mix_reshape = reshape(Mix,[Nr,Nd]);

  78. % *%TODO* :
  79. %run the FFT on the beat signal along the range bins dimension (Nr) and
  80. %normalize.

  81. sig_fft1_0=fft(Mix_reshape,Nr)./Nr;

  82. % *%TODO* :
  83. % Take the absolute value of FFT output

  84. sig_fft1_1=abs(sig_fft1_0);

  85. % *%TODO* :
  86. % Output of FFT is double sided signal, but we are interested in only one side of the spectrum.
  87. % Hence we throw out half of the samples.
  88. sig_fft1=sig_fft1_1(1:Nr/2, 1);   % 这个切片从矩阵(1024*128)变向量(1*512)了  只取一列?


  89. %plotting the range
  90. figure ('Name','Range from First FFT')
  91. subplot(1,1,1)

  92. % *%TODO* :
  93. % plot FFT output

  94. plot(sig_fft1);
  95. %plot(range_resolution*Nr/2, sig_fft1);
  96. axis ([0 200 0 1]);



  97. %% RANGE DOPPLER RESPONSE
  98. % The 2D FFT implementation is already provided here. This will run a 2DFFT
  99. % on the mixed signal (beat signal) output and generate a range doppler
  100. % map.You will implement CFAR on the generated RDM


  101. % Range Doppler Map Generation.

  102. % The output of the 2D FFT is an image that has reponse in the range and
  103. % doppler FFT bins. So, it is important to convert the axis from bin sizes
  104. % to range and doppler based on their Max values.

  105. Mix=reshape(Mix,[Nr,Nd]);

  106. % 2D FFT using the FFT size for both dimensions.
  107. sig_fft2 = fft2(Mix,Nr,Nd);

  108. % Taking just one side of signal from Range dimension.
  109. sig_fft2 = sig_fft2(1:Nr/2,1:Nd);
  110. sig_fft2 = fftshift (sig_fft2);
  111. RDM = abs(sig_fft2);
  112. RDM = 10*log10(RDM) ;

  113. %use the surf function to plot the output of 2DFFT and to show axis in both
  114. %dimensions
  115. doppler_axis = linspace(-100,100,Nd);
  116. range_axis = linspace(-200,200,Nr/2)*((Nr/2)/400);
  117. figure ('Name','2D FFT'),surf(doppler_axis,range_axis,RDM);
  118. xlabel('Speed');
  119. ylabel('Range');
  120. zlabel('db of rdm');

  121. %% CFAR implementation

  122. %Slide Window through the complete Range Doppler Map

  123. % *%TODO* :
  124. %Select the number of Training Cells in both the dimensions.

  125. Tr=10;
  126. Td=8;

  127. % *%TODO* :
  128. %Select the number of Guard Cells in both dimensions around the Cell under
  129. %test (CUT) for accurate estimation

  130. Gr=4;
  131. Gd=4;

  132. % *%TODO* :
  133. % offset the threshold by SNR value in dB

  134. off_set=1.4;


  135. % *%TODO* :
  136. %design a loop such that it slides the CUT across range doppler map by
  137. %giving margins at the edges for Training and Guard Cells.
  138. %For every iteration sum the signal level within all the training
  139. %cells. To sum convert the value from logarithmic to linear using db2pow
  140. %function. Average the summed values for all of the training
  141. %cells used. After averaging convert it back to logarithimic using pow2db.
  142. %Further add the offset to it to determine the threshold. Next, compare the
  143. %signal under CUT with this threshold. If the CUT level > threshold assign
  144. %it a value of 1, else equate it to 0.


  145. % Use RDM[x,y] as the matrix from the output of 2D FFT for implementing
  146. % CFAR

  147. RDM = RDM/max(max(RDM)); % Normalizing

  148. % *%TODO* :
  149. % The process above will generate a thresholded block, which is smaller
  150. %than the Range Doppler Map as the CUT cannot be located at the edges of
  151. %matrix. Hence,few cells will not be thresholded. To keep the map size same
  152. % set those values to 0.

  153. %Slide the cell under test across the complete martix,to note: start point
  154. %Tr+Td+1 and Td+Gd+1
  155. for i = Tr+Gr+1:(Nr/2)-(Tr+Gr)   % 这里只需要一半
  156.     for j = Td+Gd+1:(Nd)-(Td+Gd)   % trip个数 慢时间维 多普勒维
  157.         %Create a vector to store noise_level for each iteration on training cells
  158.         noise_level = zeros(1,1);
  159.         %Step through each of bins and the surroundings of the CUT
  160.         for p = i-(Tr+Gr) : i+(Tr+Gr)
  161.             for q = j-(Td+Gd) : j+(Td+Gd)
  162.                 %Exclude the Guard cells and CUT cells
  163.                 if (abs(i-p) > Gr || abs(j-q) > Gd)
  164.                     %Convert db to power   噪声水平求和
  165.                     noise_level = noise_level + db2pow(RDM(p,q));
  166.                 end
  167.             end
  168.         end
  169.         
  170.         %Calculate threshould from noise average then add the offset
  171.         threshold = pow2db(noise_level/(2*(Td+Gd+1)*2*(Tr+Gr+1)-(Gr*Gd)-1));
  172.         %Add the SNR to the threshold   db值就是加offset
  173.         threshold = threshold + off_set;
  174.         %Measure the signal in Cell Under Test(CUT) and compare against
  175.         CUT = RDM(i,j);
  176.         
  177.         if (CUT < threshold)
  178.             RDM(i,j) = 0;
  179.         else
  180.             RDM(i,j) = 1;
  181.         end
  182.         
  183.     end
  184. end

  185. RDM(RDM~=0 & RDM~=1) = 0;


  186. % *%TODO* :
  187. %display the CFAR output using the Surf function like we did for Range
  188. %Doppler Response output.
  189. % figure,surf(doppler_axis,range_axis,'replace this with output');
  190. % colorbar;
  191. figure('Name', 'CA-CFAR Filtered RDM')
  192. surf(doppler_axis,range_axis,RDM);
  193. colorbar;
  194. title( 'CA-CFAR Filtered RDM surface plot');
  195. xlabel('Speed');
  196. ylabel('Range');
  197. zlabel('Normalized Amplitude');





复制代码






fmcw测速测距01.png (331.66 KB, 下载次数: 105)

fmcw测速测距01.png

fmcw测速测距02.png (814.7 KB, 下载次数: 103)

fmcw测速测距02.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

98

帖子

200

积分

中级会员

Rank: 3Rank: 3

积分
200
沙发
发表于 2021-11-23 19:21:29 | 只看该作者
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-19 14:43 , Processed in 0.194509 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表