1 //----------------------------------------------------------------------------- 
   2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> 
   3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> 
   5 // This code is licensed to you under the terms of the GNU GPL, version 2 or, 
   6 // at your option, any later version. See the LICENSE.txt file for the text of 
   8 //----------------------------------------------------------------------------- 
  10 //----------------------------------------------------------------------------- 
  13 double CursorScaleFactor
; 
  14 int PlotGridX
, PlotGridY
, PlotGridXdefault
= 64, PlotGridYdefault
= 64; 
  16 int flushAfterWrite 
= 0; 
  17 extern pthread_mutex_t print_lock
; 
  19 static char *logfilename 
= "proxmark3.log"; 
  21 void PrintAndLog(char *fmt
, ...) 
  25         va_list argptr
, argptr2
; 
  26         static FILE *logfile 
= NULL
; 
  27         static int logging 
= 1; 
  29         // lock this section to avoid interlacing prints from different threats 
  30         pthread_mutex_lock(&print_lock
); 
  32         if (logging 
&& !logfile
) { 
  33                 logfile 
= fopen(logfilename
, "a"); 
  35                         fprintf(stderr
, "Can't open logfile, logging disabled!\n"); 
  40         int need_hack 
= (rl_readline_state 
& RL_STATE_READCMD
) > 0; 
  43                 saved_point 
= rl_point
; 
  44                 saved_line 
= rl_copy_text(0, rl_end
); 
  46                 rl_replace_line("", 0); 
  50         va_start(argptr
, fmt
); 
  51         va_copy(argptr2
, argptr
); 
  53         printf("          "); // cleaning prompt 
  59                 rl_replace_line(saved_line
, 0); 
  60                 rl_point 
= saved_point
; 
  65         if (logging 
&& logfile
) { 
  66                 vfprintf(logfile
, fmt
, argptr2
); 
  67                 fprintf(logfile
,"\n"); 
  72         if (flushAfterWrite 
== 1) { 
  76         pthread_mutex_unlock(&print_lock
);   
  79 void SetLogFilename(char *fn
) { 
  83 void iceIIR_Butterworth(int *data
, const size_t len
){ 
  87         int * output 
=  (int* ) malloc(sizeof(int) * len
);       
  88         if ( !output 
) return; 
  91         memset(output
, 0x00, len
); 
  93         size_t adjustedLen 
= len
; 
  94         float fc 
= 0.1125f
;          // center frequency 
  96     // create very simple low-pass filter to remove images (2nd-order Butterworth) 
  97     float complex iir_buf
[3] = {0,0,0}; 
  98     float b
[3] = {0.003621681514929,  0.007243363029857, 0.003621681514929}; 
  99     float a
[3] = {1.000000000000000, -1.822694925196308, 0.837181651256023}; 
 101     float sample           
= 0;      // input sample read from array 
 102     float complex x_prime  
= 1.0f
;   // save sample for estimating frequency 
 105         for (i 
= 0; i 
< adjustedLen
; ++i
) { 
 109         // remove DC offset and mix to complex baseband 
 110         x 
= (sample 
- 127.5f
) * cexpf( _Complex_I 
* 2 * M_PI 
* fc 
* i 
); 
 112         // apply low-pass filter, removing spectral image (IIR using direct-form II) 
 113         iir_buf
[2] = iir_buf
[1]; 
 114         iir_buf
[1] = iir_buf
[0]; 
 115         iir_buf
[0] = x 
- a
[1]*iir_buf
[1] - a
[2]*iir_buf
[2]; 
 116         x          
= b
[0]*iir_buf
[0] + 
 120         // compute instantaneous frequency by looking at phase difference 
 121         // between adjacent samples 
 122         float freq 
= cargf(x
*conjf(x_prime
)); 
 123         x_prime 
= x
;    // retain this sample for next iteration 
 125                 output
[i
] =(freq 
> 0) ? 127 : -127; 
 129         //memcpy(data, output, adjustedLen); 
 130         for (j
=0; j
<adjustedLen
; ++j
) 
 136 void iceSimple_Filter(int *data
, const size_t len
, uint8_t k
){ 
 137 // ref: http://www.edn.com/design/systems-design/4320010/A-simple-software-lowpass-filter-suits-embedded-system-applications 
 139 #define FILTER_SHIFT 4  
 141         int32_t filter_reg 
= 0; 
 142         int16_t input
, output
; 
 143         int8_t shift 
= (k 
<=8 ) ? k 
: FILTER_SHIFT
; 
 145         for (int i 
= 0; i 
< len
; ++i
){ 
 148                 // Update filter with current sample 
 149                 filter_reg 
= filter_reg 
- (filter_reg 
>> shift
) + input
; 
 151                 // Scale output for unity gain 
 152                 output 
= filter_reg 
>> shift
; 
 157 float complex cexpf (float complex Z
) 
 160   double rho 
= exp (__real__ Z
); 
 161   __real__ Res 
= rho 
* cosf(__imag__ Z
); 
 162   __imag__ Res 
= rho 
* sinf(__imag__ Z
);