#!/usr/bin/env python

import sys

#doppler 
#The relationship between observed frequency f' and emitted frequency f is given by:
#        f' = f*v/(v +/- v_s)
#
#   where
#        v , is the speed of waves in the medium 
#		(in air at T degrees Celsius, this is 332*sqrt(1 + T/273) m/s)
#               (for light this is c = 3.0e8 m/s = 10.8e8 km/hour)
#        v_s , is the velocity of the source (the object emitting the wave)
#doppler_shift = f*(1-c/(c + v_s)= f*(c+v_s - c)/(c+v_s)=f*v_s/(c+v_s)
#max_speed_airplane = 900 km/hour = 250 m/sec
#max_speed_kmh=900 #km/hour
#v_s=max_speed_kmh/3.6 #m/s
#f=100.0e6 #freq of source in Hz
#c = 3.0e8 #speed of light in m/s
#max_dopllershift_Hz=f*v_s/(c+v_s)

def calc_normalized_dopplershift(speed, freq,samplerate):
  v_s=speed/3.6 #transfor km/hour to m/s
  c = 3.0e8 #speed of light in m/s
  dopplershift_hz=freq*v_s/(c+v_s)
  print "#max dopplershift Hz =",dopplershift_hz
  dopplershift_normalized=dopplershift_hz/samplerate
  print "#max_dopplershift normalized =",dopplershift_normalized
  return dopplershift_normalized

def main():
    sys.stdout.write("#!/bin/sh \n")
    max_speed=1000 #km/hour is max speed airplane
    transmit_freq=100.0e6 #transmit freq of station we are using as source
    samplerate=320.0e3 
    max_dopplershift=calc_normalized_dopplershift(speed=max_speed,freq=transmit_freq,samplerate=samplerate)
    window = 256*1024#*16 # 2048*256 #5*3000000
    #window = 250000
    min_range=0
    max_range = 300
    ndopplers=16*8
    gifs = []
    samplerate=320000
    secs_per_timestep=2 #0.5*float(window)/samplerate2 #2
    samples_per_timestep=int(float(samplerate*secs_per_timestep))
    ntimesteps=10#10 #int((30000000 - window -320000)/samples_per_timestep)-2
    print "#window: nsamples = ",window," time = ", window/samplerate, " secs"
    print "#range min",min_range,"max",max_range," samples"
    print "#range min",3.0e5*min_range/samplerate,"max",3.0e5*max_range/samplerate,"km"
    for t in range(0,ntimesteps, 1):
        #sim_fn  = "mdvh_bingo4_sim.%04d" % (t,)
        ref_fn  = "ref_320ksps.raw" 
        scat_fn  = "scat_320ksps.raw" 
        xref_fn = "mdvh_wiener4_xref.%04d" % (t,)
        ppm_fn  = "mdvh_wiener4_i.%04d.ppm" % (t,)
        gif_fn  = "mdvh_wiener4_i.%04d.gif" % (t,)
        gifs.append(gif_fn)
        skipsamples=samples_per_timestep*(t+1)
        #sys.stdout.write("./sim-airplane3 bingo_320ksps.raw -o %s -S %d\n" % (sim_fn, t))
        #sys.stdout.write("./mdvh-xambi-crosscorr-wiener -o %s -s %d -S 0 -M %d -w %d -n %d -d 0.0001 %s %s\n" % (
        #    xref_fn, skipsamples,max_range, window,ndopplers,ref_fn,scat_fn))

        sys.stdout.write("./mdvh-xambi-crosscorr-wiener -m %d -M %d -w %d -o %s -s %d -d %r -n %d  %s %s\n" % (
            min_range, max_range, window, xref_fn, skipsamples, max_dopplershift, ndopplers, ref_fn,scat_fn))

        sys.stdout.write("./plot_xambi_tool_log.m %s %s\n" % (xref_fn, ppm_fn))
        sys.stdout.write("convert -resize 800%% %s %s\n" % (ppm_fn, gif_fn))

    sys.stdout.write("gifsicle -d 50 %s >mdvh_wiener4_animated.gif\n" % (' '.join(gifs)))

if __name__ == '__main__':
    main()
