/ 8_Utils / Python / CSV_radar_2.py
CSV_radar_2.py
 1  import numpy as np
 2  import pandas as pd
 3  import math
 4  
 5  def generate_small_radar_csv(filename="small_test_radar_data.csv"):
 6      """
 7      Generate a smaller, faster-to-process radar CSV
 8      """
 9      # Reduced parameters for faster processing
10      num_long_chirps = 8    # Reduced from 16
11      num_short_chirps = 8   # Reduced from 16  
12      samples_per_chirp = 128 # Reduced from 512
13      fs_adc = 400e6
14      
15      targets = [
16          {'range': 3000, 'velocity': 25, 'snr': 40},
17          {'range': 5000, 'velocity': -15, 'snr': 35},
18      ]
19      
20      data = []
21      chirp_number = 0
22      timestamp_ns = 0
23      
24      # Generate Long Chirps
25      for chirp in range(num_long_chirps):
26          for sample in range(samples_per_chirp):
27              i_val = np.random.normal(0, 3)
28              q_val = np.random.normal(0, 3)
29              
30              # Add targets
31              for target in targets:
32                  range_bin = int(target['range'] / 40)
33                  doppler_phase = 2 * math.pi * target['velocity'] * chirp / 50
34                  
35                  if abs(sample - range_bin) < 5:
36                      amplitude = target['snr'] * (8000 / target['range'])
37                      phase = 2 * math.pi * sample / 30 + doppler_phase
38                      i_val += amplitude * math.cos(phase)
39                      q_val += amplitude * math.sin(phase)
40              
41              magnitude_squared = i_val**2 + q_val**2
42              
43              data.append({
44                  'timestamp_ns': timestamp_ns,
45                  'chirp_number': chirp_number,
46                  'chirp_type': 'LONG',
47                  'sample_index': sample,
48                  'I_value': int(i_val),
49                  'Q_value': int(q_val),
50                  'magnitude_squared': int(magnitude_squared)
51              })
52              
53              timestamp_ns += int(1e9 / fs_adc)
54          
55          chirp_number += 1
56          timestamp_ns += 137000
57      
58      # Generate Short Chirps
59      for chirp in range(num_short_chirps):
60          for sample in range(samples_per_chirp):
61              i_val = np.random.normal(0, 3)
62              q_val = np.random.normal(0, 3)
63              
64              for target in targets:
65                  range_bin = int(target['range'] / 60)
66                  doppler_phase = 2 * math.pi * target['velocity'] * (chirp + 2) / 40
67                  
68                  if abs(sample - range_bin) < 4:
69                      amplitude = target['snr'] * 0.6 * (6000 / target['range'])
70                      phase = 2 * math.pi * sample / 25 + doppler_phase
71                      i_val += amplitude * math.cos(phase)
72                      q_val += amplitude * math.sin(phase)
73              
74              magnitude_squared = i_val**2 + q_val**2
75              
76              data.append({
77                  'timestamp_ns': timestamp_ns,
78                  'chirp_number': chirp_number,
79                  'chirp_type': 'SHORT', 
80                  'sample_index': sample,
81                  'I_value': int(i_val),
82                  'Q_value': int(q_val),
83                  'magnitude_squared': int(magnitude_squared)
84              })
85              
86              timestamp_ns += int(1e9 / fs_adc)
87          
88          chirp_number += 1
89          timestamp_ns += 174500
90      
91      df = pd.DataFrame(data)
92      df.to_csv(filename, index=False)
93      print(f"Generated small CSV: {filename}")
94      print(f"Total samples: {len(df)}")
95      return df
96  
97  generate_small_radar_csv()