00001
00005 #include <math.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008
00009 #include <mgk60.h>
00010 #include <simgui.h>
00011
00012 #include "System.h"
00013 #include "fft.h"
00014
00015
00016 #ifndef M_PI
00017 #define M_PI 3.1415926
00018 #endif
00019 #ifndef NULL
00020 #define NULL 0
00021 #endif
00022 #ifndef TRUE
00023 #define TRUE 1
00024 #endif
00025 #ifndef FALSE
00026 #define FALSE 0
00027 #endif
00028
00029 #ifndef min
00030 #define min(x,y) (((x) < (y)) ? (x) : (y))
00031 #endif
00032 #ifndef max
00033 #define max(x,y) (((x) > (y)) ? (x) : (y))
00034 #endif
00035 #ifndef sqr
00036 #define sqr(x) ((x) * (x))
00037 #endif
00038
00039
00040 #define CT_SINAMP "Chan %d sine ampl"
00041 #define CT_SINFREQ "Chan %d sine freq"
00042 #define CT_NOISEAMP "Chan %d noise ampl"
00043
00044
00045 #define ENV_RESET "Reset envelopes"
00046 #define FILT_RESET "Reset filters"
00047
00048 #if 0
00049
00050
00051
00052
00053 void template_script(void)
00054 {
00055 mgk_data_type type;
00056 float *data;
00057 data = mgk_receive(0,&type);
00058 if(data && (type == (T_FLOAT | T_BUFFER))) {
00059 int size = mgk_buffer_size(data) / sizeof(float);
00060
00061
00062
00063
00064 mgk_propagate(0,data,(T_FLOAT | T_BUFFER));
00065 }
00066 }
00067 #endif
00068
00069
00070
00071
00072
00073
00074
00075 void datagen_script(void)
00076 {
00077 mgk_data_type type;
00078 datagen_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00079 printf("gen\n");
00080 fflush(stdout);
00081 if(cxt && (type & T_BUFFER)) {
00082 double samp;
00083 double sfreq;
00084 double namp;
00085 float *data;
00086 char cname[100];
00087 sprintf(cname,CT_SINAMP,cxt->Channel);
00088 samp = gui_get_control_value(cname);
00089 sprintf(cname,CT_SINFREQ,cxt->Channel);
00090 sfreq = gui_get_control_value(cname);
00091 sprintf(cname,CT_NOISEAMP,cxt->Channel);
00092 namp = gui_get_control_value(cname);
00093 data = mgk_allocate_buffer((sizeof(float) * cxt->Size),0);
00094 if(data) {
00095 int i;
00096 for(i = 0; i < cxt->Size; i++) {
00097 data[i] = (float)(
00098 samp * cos(2.0 * M_PI * sfreq * i / cxt->Frequency) +
00099 namp * (double)(rand() - (RAND_MAX / 2)) * (2.0 / RAND_MAX));
00100 }
00101 mgk_propagate(0,data,(T_FLOAT | T_BUFFER));
00102 }
00103 }
00104 }
00105
00106 static
00107 #if defined(__GNUC__) || defined(__cplusplus)
00108 inline
00109 #endif
00110 int
00111 ilog2(int value)
00112 {
00113 int ilog;
00114 for(ilog = 0; (1 << ilog) != value; ilog++) {
00115 if((1 << ilog) == 0) {
00116 return(-1);
00117 }
00118 }
00119 return(ilog);
00120 }
00121
00122
00123
00124
00125
00126 void fft_script(void)
00127 {
00128 mgk_data_type rtype,itype;
00129 int logsize;
00130 float *re = mgk_receive(0,&rtype);
00131 float *im = mgk_receive(1,&itype);
00132 printf("fft\n");
00133 fflush(stdout);
00134 if((re && im) &&
00135 (rtype == itype) &&
00136 (rtype == (T_BUFFER | T_FLOAT)) &&
00137 (mgk_buffer_size(re) == mgk_buffer_size(im)) &&
00138 ((logsize = ilog2(mgk_buffer_size(re) / sizeof(float))) >= 3)) {
00139 fft32(re,im,logsize,0);
00140 mgk_propagate(0,re,rtype);
00141 mgk_propagate(1,im,itype);
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 void splitter_script(void)
00152 {
00153 mgk_data_type rtype,itype;
00154 int size;
00155 float *re = mgk_receive(0,&rtype);
00156 float *im = mgk_receive(1,&itype);
00157 printf("split\n");
00158 fflush(stdout);
00159 if((re && im) &&
00160 (rtype == itype) &&
00161 (rtype == (T_BUFFER | T_FLOAT)) &&
00162 (mgk_buffer_size(re) == mgk_buffer_size(im)) &&
00163 ((size = (mgk_buffer_size(re) / sizeof(float))) >= 4) &&
00164 ((size & 1) == 0)) {
00165 int hsize = size / 2;
00166 float *re1 = mgk_allocate_buffer((sizeof(float) * hsize),0);
00167 float *im1 = mgk_allocate_buffer((sizeof(float) * hsize),0);
00168 float *re2 = mgk_allocate_buffer((sizeof(float) * hsize),0);
00169 float *im2 = mgk_allocate_buffer((sizeof(float) * hsize),0);
00170 if(re1 && im1 && re2 && im2) {
00171 int i;
00172 for(i = 0; i < hsize; i++) {
00173 int j = i ? (size - i) : 0;
00174 re1[i] = 0.5f * (re[i] + re[j]);
00175 re2[i] = 0.5f * (re[i] - re[j]);
00176 im1[i] = 0.5f * (im[i] - im[j]);
00177 im2[i] = 0.5f * (im[i] + im[j]);
00178 }
00179 mgk_propagate(0,re1,(T_FLOAT | T_BUFFER));
00180 mgk_propagate(1,im1,(T_FLOAT | T_BUFFER));
00181 mgk_propagate(2,re2,(T_FLOAT | T_BUFFER));
00182 mgk_propagate(3,im2,(T_FLOAT | T_BUFFER));
00183 }
00184 }
00185 }
00186
00187
00188
00189
00190
00191 void abs_phase_script(void)
00192 {
00193 mgk_data_type rtype,itype;
00194 int size;
00195 float *re = mgk_receive(0,&rtype);
00196 float *im = mgk_receive(1,&itype);
00197 printf("abs+phase\n");
00198 fflush(stdout);
00199 if((re && im) &&
00200 (rtype == itype) &&
00201 (rtype == (T_BUFFER | T_FLOAT)) &&
00202 (mgk_buffer_size(re) == mgk_buffer_size(im)) &&
00203 ((size = (mgk_buffer_size(re) / sizeof(float))) > 0)) {
00204 int i;
00205 for(i = 0; i < size; i++) {
00206 float magn = (float)(sqrt((re[i] * re[i]) + (im[i] * im[i])));
00207 float phase = (float)(atan2(im[i],re[i]) * 180.0 / M_PI);
00208 re[i] = magn;
00209 im[i] = phase;
00210 }
00211 mgk_propagate(0,re,rtype);
00212 mgk_propagate(1,im,itype);
00213 }
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 void envelope_script(void)
00228 {
00229 mgk_data_type type;
00230 envelope_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00231 printf("env\n");
00232 fflush(stdout);
00233 if(cxt && (type & T_BUFFER)) {
00234 int i,size,do_reset = FALSE;
00235 float *data = mgk_receive(0,&type);
00236 if((data) &&
00237 (type == (T_FLOAT | T_BUFFER)) &&
00238 ((size = mgk_buffer_size(data) / sizeof(float)) > 0)) {
00239 if(!cxt->Buffer) {
00240 cxt->Buffer = mgk_allocate_buffer((sizeof(float) * size),0);
00241 if(!cxt->Buffer) {
00242 return;
00243 }
00244 mgk_protect_buffer(cxt->Buffer);
00245 do_reset = TRUE;
00246 }
00247 if(gui_get_button_value(ENV_RESET)) {
00248 (*(cxt->Reset))++;
00249 }
00250 if(*(cxt->Reset) != cxt->LastReset) {
00251 do_reset = TRUE;
00252 cxt->LastReset = *(cxt->Reset);
00253 }
00254 if(do_reset) {
00255 for(i = 0; i < size; i++) {
00256 ((float *)(cxt->Buffer))[i] = data[i];
00257 }
00258 }
00259 else {
00260 for(i = 0; i < size; i++) {
00261 data[i] = ((float *)(cxt->Buffer))[i] =
00262 max(data[i],((float *)(cxt->Buffer))[i]);
00263 }
00264 }
00265 mgk_propagate(0,data,(T_BUFFER | T_FLOAT));
00266 }
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 void lpfilter_script(void)
00279 {
00280 mgk_data_type type;
00281 lpfilter_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00282 printf("lpfilter\n");
00283 fflush(stdout);
00284 if(cxt && (type & T_BUFFER)) {
00285 int i,size,do_reset = FALSE;
00286 float *data = mgk_receive(0,&type);
00287 if((data) &&
00288 (type == (T_FLOAT | T_BUFFER)) &&
00289 ((size = mgk_buffer_size(data) / sizeof(float)) > 0)) {
00290 if(!cxt->Buffer) {
00291 cxt->Buffer = mgk_allocate_buffer((sizeof(float) * size),0);
00292 if(!cxt->Buffer) {
00293 return;
00294 }
00295 mgk_protect_buffer(cxt->Buffer);
00296 do_reset = TRUE;
00297 }
00298 if(gui_get_button_value(FILT_RESET)) {
00299 (*cxt->Reset)++;
00300 }
00301 if(*cxt->Reset != cxt->LastReset) {
00302 do_reset = TRUE;
00303 cxt->LastReset = *cxt->Reset;
00304 }
00305 if(do_reset) {
00306 for(i = 0; i < size; i++) {
00307 ((float *)(cxt->Buffer))[i] = data[i];
00308 }
00309 }
00310 else {
00311 for(i = 0; i < size; i++) {
00312 data[i] = ((float *)(cxt->Buffer))[i] = (float)(
00313 data[i] * cxt->Factor +
00314 ((float *)(cxt->Buffer))[i] * (1.0 - cxt->Factor));
00315 }
00316 }
00317 mgk_propagate(0,data,(T_BUFFER | T_FLOAT));
00318 }
00319 }
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 void plotter_script(void)
00331 {
00332 char varname[100];
00333 mgk_data_type type;
00334 plotter_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00335 printf("plot\n");
00336 fflush(stdout);
00337 if(cxt && (type & T_BUFFER)) {
00338 int size;
00339 float *data = mgk_receive(0,&type);
00340 if((data) &&
00341 (type == (T_FLOAT | T_BUFFER)) &&
00342 ((size = mgk_buffer_size(data) / sizeof(float)) > 0)) {
00343 sprintf(varname,"Chan%02d",cxt->Channel);
00344 gui_clear_data(varname,cxt->DBase);
00345 gui_append_data(varname,cxt->DBase,data,size);
00346 }
00347 }
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 void real2complex_script(void)
00360 {
00361 mgk_data_type type;
00362 float *data;
00363 printf("real2complex\n");
00364 fflush(stdout);
00365 data = mgk_receive(0,&type);
00366 if(data && (type == (T_FLOAT | T_BUFFER))) {
00367 int i,size = mgk_buffer_size(data) / sizeof(float);
00368 float *zero = mgk_allocate_buffer((sizeof(float) * size),0);
00369 if(zero) {
00370 for(i = 0; i < size; i++) {
00371 zero[i] = 0.0f;
00372 }
00373 mgk_propagate(0,data,(T_FLOAT | T_BUFFER));
00374 mgk_propagate(1,zero,(T_FLOAT | T_BUFFER));
00375 }
00376 }
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 void guisetup_script(void)
00393 {
00394 mgk_data_type type;
00395 guisetup_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00396 if(cxt && (type & T_BUFFER)) {
00397 int i;
00398 char name[100];
00399 gui_define_button(ENV_RESET);
00400 gui_define_button(FILT_RESET);
00401 for(i = 1; i <= cxt->NumChannels; i++) {
00402 sprintf(name, CT_SINAMP, i);
00403 gui_define_control(name, 0.1, 10.0, 1.0);
00404 sprintf(name, CT_SINFREQ, i);
00405 gui_define_control(name, 0.0, cxt->SampRate * 0.49, cxt->SampRate * 0.05 * i);
00406 sprintf(name, CT_NOISEAMP, i);
00407 gui_define_control(name, 0.1, 10.0, 1.0);
00408 }
00409 }
00410 mgk_set_node_priority(mgk_current_node(),0);
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 void dbsetup_script(void)
00425 {
00426 mgk_data_type type;
00427 dbsetup_script_context *cxt = mgk_node_context(mgk_current_node(),&type);
00428 if(cxt && (type & T_BUFFER)) {
00429 int i;
00430 int size = cxt->IsFrequency ?
00431 cxt->Size / 2 :
00432 cxt->Size;
00433 char *xname = cxt->IsFrequency ?
00434 "Frequency" :
00435 "Time";
00436 double step = cxt->IsFrequency ?
00437 cxt->SampRate / cxt->Size :
00438 1.0 / cxt->SampRate;
00439 float *data = mgk_allocate_buffer((sizeof(float) * size),FALSE);
00440 for(i = 0; i < size; i++) {
00441 data[i] = (float)i * (float)step;
00442 }
00443 gui_setup_database(cxt->Name,xname);
00444 gui_clear_database(cxt->Name);
00445 gui_append_data(xname, cxt->Name, data, size);
00446 }
00447 mgk_set_node_priority(mgk_current_node(),0);
00448 }