forked from noobmartin/Open-source-SAR-simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sar_simulator.c
executable file
·235 lines (203 loc) · 6.09 KB
/
sar_simulator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Author: Alexander Rajula
* Contact: alexander@rajula.org
*
* This simulator can either simulate and process simulated data,
* or read pre-existing radar data from file, and then process it.
*
* The simulator utilizes standard SAR imaging and filtering algorithms, and the
* processing chain typically looks something like this:
* create an empty scene -> generate chirp waveform -> generate matched chirp waveform ->
* pulse compress single chirp waveform -> insert original (uncompressed) waveform in the middle of the scene ->
* employ radar scanning algorithm -> employ RFI filtering (if needed) ->
* employ pulse compression (if needed) -> employ GBP algorithm -> employ apodization (if needed) ->
* calculate 2D FFT of GBP image -> write *all* data to file
*
* This file (sar_simulator.c) contains no algorithms, it just helps the user input data needed for simulation and
* processing.
*/
#include "sar_simulator.h"
#include "cinsnowfilters.h"
int main(int argc, char** argv){
radar_variables variables;
matrix* data = malloc(sizeof(matrix));
memset(data, 0, sizeof(matrix));
strcpy(data->name, "metadata");
printf("Do you wish to simulate or process radar data? (s/p): ");
variables.mode = getchar();
int ret;
if(variables.mode == 'p'){
printf("Please enter file name of raw data: ");
ret = scanf("%s", variables.radar_data_filename);
if(ret == EOF){
printf("Invalid input detected, closing.\n");
return 0;
}
if(!read_radar_file(data, &variables)){
printf("Failed to read radar data, closing.\n");
return 0;
}
process_data(data, &variables);
}
else if(variables.mode == 's'){
/*
printf("Simulate with real or complex values? (r/c): ");
ret = scanf("%s", variables.real_or_complex_simulation);
if(*variables.real_or_complex_simulation != 'r')
*variables.real_or_complex_simulation = 'c';
*/
ret = simulate(data, &variables);
if(ret == -1)
return 0;
process_data(data, &variables);
}
else{
printf("Mode not recognized - exiting.\n");
return 0;
}
build_metadata(data, &variables);
ret = write_data(data, &variables);
//free_memory(data);
return 0;
}
void free_memory(matrix* data_matrix){
matrix* ptr = data_matrix;
matrix* next = ptr->next;
while(ptr != NULL){
if(ptr->data != NULL)
free(ptr->data);
next = ptr->next;
free(ptr);
ptr = next;
}
}
int simulate(matrix* data, radar_variables* variables){
chirp_generator(data, variables);
chirp_matched_generator(data, variables);
matrix* meta_chirp = get_matrix(data, "chirp");
complex double* chirp = meta_chirp->data;
matrix* meta_match = get_matrix(data, "match");
complex double* match = meta_match->data;
matrix* meta_chirp_fft = get_last_node(data);
meta_chirp_fft->data = malloc(meta_chirp->rows*sizeof(complex double));
complex double* chirp_fft = meta_chirp_fft->data;
meta_chirp_fft->rows = meta_chirp->rows;
meta_chirp_fft->cols = meta_chirp->cols;
strcpy(meta_chirp_fft->name, "chirp_fft");
matrix* meta_match_fft = get_last_node(data);
meta_match_fft->data = malloc(meta_match->rows*sizeof(complex double));
complex double* match_fft = meta_match_fft->data;
meta_match_fft->rows = meta_chirp->rows;
meta_match_fft->cols = meta_chirp->cols;
strcpy(meta_match_fft->name, "match_fft");
fft_waveform(meta_chirp->rows, chirp, chirp_fft);
fft_waveform(meta_match->rows, match, match_fft);
pulse_compress_signal(data, variables);
printf("Compressed pulse resolution: %lfm\n", calculate_compressed_pulse_resolution(data, variables));
insert_waveform_in_scene(data,variables);
radar_imager(data, variables);
/*
printf("Do you want to simulate radio-frequency interference (y/n)? ");
do{
ret = scanf("%c", &pc);
if(pc == 'y')
break;
else if(pc == 'n')
break;
}while(1);
if(pc == 'y'){
printf("Adding GSM interference ... ");
generate_gsm_interference(data, variables);
printf("done.\n");
}
*/
return 0;
}
void process_data(matrix* data, radar_variables* variables){
char pc = 0;
int ret;
printf("Do you want to employ CinSnow filtering to radar image (y/n)? ");
do{
ret = scanf("%c", &pc);
if(pc == 'y')
break;
else if(pc == 'n')
break;
}while(1);
if(pc == 'y'){
printf("Running CinSnow filters ... ");
cinsnowfilters(data, variables);
printf("done.\n");
}
/*
printf("Do you want to employ RFI suppression (y/n)? ");
do{
ret = scanf("%c", &pc);
if(pc == 'y')
break;
else if(pc == 'n')
break;
}while(1);
if(pc == 'y'){
printf("Suppressing RFI ... ");
rfi_suppression(data, variables);
printf("done.\n");
}
else if(pc == 'n'){
if(!data->unfiltered_radar_image){
data->unfiltered_radar_image = malloc(variables->ncols*variables->nrows*sizeof(complex double));
memcpy(data->unfiltered_radar_image, data->radar_image, variables->ncols*variables->nrows*sizeof(complex double));
}
}
*/
printf("Do you want to enable pulse compression (y/n)? ");
do{
ret = scanf("%c", &pc);
if(pc == 'y')
break;
else if(pc == 'n')
break;
}while(1);
if(pc == 'y'){
printf("Pulse-compressing image ... ");
pulse_compress_image(data, variables);
printf("done.\n");
}
gbp(data, variables);
printf("Generating 2D FFT of GBP image ... ");
gbp_fft(data, variables);
printf("done.\n");
}
matrix* get_matrix(matrix* data, const char* name){
if(data == NULL)
return NULL;
matrix* ptr = data;
while(ptr != NULL){
if( (strcmp(name, ptr->name) == 0) )
return ptr;
ptr = ptr->next;
};
return NULL;
}
matrix* get_last_node(matrix* data){
if(data == NULL)
return NULL;
matrix* ptr = data;
do{
if(ptr->next == NULL)
break;
ptr = ptr->next;
}while(1);
ptr->next = malloc(sizeof(matrix));
memset(ptr->next, 0, sizeof(matrix));
return ptr->next;
}
void build_metadata(matrix* data, radar_variables* variables){
matrix* ptr = data->next;
unsigned int elements = 0;
while(ptr != NULL){
elements++;
ptr = ptr->next;
}
data->data = malloc(elements*sizeof(complex double));
}