def main(): data = loadmat('fdcorr_demo') plot_points = fdcorr(data['rx_seg'], ca_code.generate(prn=26, repeats=2), data['frange']) plot(plot_points)
def main(): code_1 = ca_code.generate(prn=1) | np.roll(ca_code.generate(prn=2), 100) code_2 = ca_code.generate(prn=1) # code_2 = np.roll(code_2, 100) # fftshift(ifft(fft(a,corrLength).*conj(fft(b,corrLength)))) q = 2 code_1 = sfft_aliasing.execute(code_1, q) code_2 = sfft_aliasing.execute(code_2, q) code_1_fft = fourier_transforms.fft(code_1) code_2_fft = fourier_transforms.fft(code_2) multiplied = code_1_fft * np.conj(code_2_fft) print multiplied.size params = Parameters( n=multiplied.size/2, k=1, B_k_location=2, B_k_estimation=2, estimation_loops=8, location_loops=5, loop_threshold=4, tolerance_location=1e-8, tolerance_estimation=1e-8 ) result = sfft_inverse.execute(params=params, x=multiplied) result = fourier_transforms.fftshift(result) result_actual = fourier_transforms.ifft(multiplied) result_actual = fourier_transforms.fftshift(result_actual) print 'sfft size', result.size print 'original size', result_actual.size fig = plt.figure() ax = fig.gca() ax.plot(np.linspace(-1, 1, result.size), np.abs(result)) ax.plot(np.linspace(-1, 1, result_actual.size), np.abs(result_actual)) plt.show()
def main(): # (a) Input code = ca_code.generate(prn=1, repeats=10) # rx = np.roll(ca_code.generate(prn=1, repeats=10), -10230/6*1) | ca_code.generate(prn=2, repeats=10) rx = np.roll(ca_code.generate(prn=1, repeats=10), 1000) | ca_code.generate(prn=2, repeats=10) noise_std = 1 noise = np.random.normal( scale=noise_std, size=code.size, ) rx = np.add(rx, noise) # (b) Aliasing q = 2 code_aliased = sfft_aliasing.execute(code, q) rx_aliased = sfft_aliasing.execute(rx, q) # (c) FFT code_f = fft(code_aliased) rx_f = fft(rx_aliased) # (d) Multiply code_rx_f = np.multiply(np.conjugate(code_f), rx_f) # (e) IFFT code_rx = np.real(ifft(code_rx_f)) print 'arg max is', np.argmax(code_rx) plt.figure('Local C/A code') plt.step(np.arange(code.size), code) plt.xlim(0, code.size-1) plt.title('Local C/A code') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Received signal') plt.plot(rx) plt.xlim(0, code.size-1) plt.title('Received signal') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Aliased Local C/A code') plt.step(np.arange(code_aliased.size), code_aliased) plt.xlim(0, code_aliased.size-1) plt.title('Local C/A code (Aliased)') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Aliased Received signal') plt.plot(rx_aliased) plt.xlim(0, rx_aliased.size-1) plt.title('Received signal (Aliased)') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Local CA code') plt.plot(np.abs(fftshift(code_f))) plt.title('FFT of Local C/A code') plt.xlabel('Frequency') plt.xlim(1000, 4000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Received signal') plt.plot(np.abs(fftshift(rx_f))) plt.title('FFT of Received signal') plt.xlabel('Frequency') plt.xlim(1000, 4000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Multiply') plt.plot(np.abs(fftshift(code_rx_f))) plt.title('Multiply') plt.xlabel('Frequency') plt.xlim(1500, 3500) plt.ylim(0, 100000) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('IFFT') plt.plot(code_rx) plt.title('IFFT') plt.xlim(0, code_rx.size-1) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.show()
def check_correctness(prn, expected): actual = ca_code.generate(prn=prn) assert_array_equal(actual[0, 0:10], expected)
def acquisition(x, settings, plot_graphs=False): # Calculate number of samples per spreading code (corresponding to 1ms of data) samples_per_code = int(round(settings['sampling_frequency'] * settings['code_length'] / float(settings['code_frequency']))) subsamples_per_code = samples_per_code / settings['subsampling_factor'] print 'There are %d samples per code' % samples_per_code x_1 = x[0:samples_per_code] x_2 = x[samples_per_code:2*samples_per_code] x_0dc = x - np.mean(x) # Calculate sampling period sampling_period = 1.0 / settings['sampling_frequency'] phases = np.arange(0, samples_per_code) * 2 * np.pi * sampling_period # Calculate number of frequency bins assert settings['acquisition_search_frequency_band'] % settings['acquisition_search_frequency_step'] == 0, \ 'acquisition_search_frequency_band should be divisible by acquisition_search_frequency_step' n_frequency_bins = settings['acquisition_search_frequency_band'] / settings['acquisition_search_frequency_step'] print 'There are %d frequency bins' % n_frequency_bins # todo generate ca table # SFFT: Aliasing all_results = np.zeros(shape=(n_frequency_bins, samples_per_code / settings['subsampling_factor'])) # all_results = np.zeros(shape=(n_frequency_bins, samples_per_code)) # Generate all frequency bins frequency_bins = settings['intermediate_frequency'] - \ (settings['acquisition_search_frequency_band'] / 2) + \ settings['acquisition_search_frequency_step'] * np.arange(n_frequency_bins) # Allocate output dictionary output = { 'frequency_shifts': np.zeros(settings['satellites_to_search'].size), 'code_shifts': np.zeros(settings['satellites_to_search'].size), 'peak_ratios': np.zeros(settings['satellites_to_search'].size), 'found': np.zeros(settings['satellites_to_search'].size, dtype=np.bool), } for idx, prn in enumerate(settings['satellites_to_search']): print 'Acquiring satellite PRN = %d' % prn ca_code_t = ca_code.generate(prn=prn).repeat(samples_per_code/1023) # SFFT: Aliasing ca_code_t = sfft_aliasing.execute(ca_code_t, settings['subsampling_factor']) ca_code_f = np.conj(np.fft.fft(ca_code_t)) # Scan Doppler frequencies for freq_bin_i in range(n_frequency_bins): # Generate local sine and cosine carriers carrier_sin = np.sin(frequency_bins[freq_bin_i] * phases) carrier_cos = np.cos(frequency_bins[freq_bin_i] * phases) # Demodulation IQ_1 = carrier_sin*x_1 + 1j*carrier_cos*x_1 IQ_2 = carrier_sin*x_2 + 1j*carrier_cos*x_2 # SFFT: Aliasing IQ_1 = sfft_aliasing.execute(IQ_1, settings['subsampling_factor']) IQ_2 = sfft_aliasing.execute(IQ_2, settings['subsampling_factor']) # Baseband signal to frequency domain IQ_1_freq = np.fft.fft(IQ_1) IQ_2_freq = np.fft.fft(IQ_2) # Convolve conv_code_IQ_1 = IQ_1_freq * ca_code_f conv_code_IQ_2 = IQ_2_freq * ca_code_f # IFFT to obtain correlation corr_result_1 = np.abs(np.fft.ifft(conv_code_IQ_1)) ** 2 corr_result_2 = np.abs(np.fft.ifft(conv_code_IQ_2)) ** 2 # assert all_results[freq_bin_i, :].shape == corr_result_1.shape == corr_result_2.shape if np.max(corr_result_1) > np.max(corr_result_2): all_results[freq_bin_i, :] = corr_result_1 else: all_results[freq_bin_i, :] = corr_result_2 # Peak location for each Doppler shift peak_values = all_results.max(axis=1) assert all_results.max() in peak_values frequency_shift = peak_values.argmax() print 'Frequency shift is %d' % frequency_shift correct_frequency_result = all_results[frequency_shift] code_shift = correct_frequency_result.argmax() print 'Code shift is %d' % code_shift assert all_results.max() == all_results[frequency_shift][code_shift] peak_value = all_results[frequency_shift][code_shift] print 'Peak value = %f' % peak_value # if plot_graphs: # print np.arange(1023*16/settings['subsampling_factor']).reshape((1, -1)).shape, frequency_bins.shape, all_results.shape # # fig = plt.figure() # ax = fig.gca(projection='3d') # surf = ax.plot_surface( # X=np.arange(1023*16/settings['subsampling_factor']).reshape((1, -1)), # Y=frequency_bins.reshape((-1, 1)), # Z=all_results, # rstride=1, # cstride=1, # cmap=matplotlib.cm.coolwarm, # linewidth=0, # antialiased=False, # ) # plt.show() # Calculate code phase range samples_per_code_chip = int(round(settings['sampling_frequency'] / settings['code_frequency'] / 2)) excluded_range_1 = code_shift - samples_per_code_chip excluded_range_2 = code_shift + samples_per_code_chip print 'code_shift', code_shift print 'samples_per_code', samples_per_code print 'samples_per_code_chip', samples_per_code_chip print 'excluded_range_1', excluded_range_1 print 'excluded_range_2', excluded_range_2 if excluded_range_1 < 1: code_phase_range = np.arange(excluded_range_2, subsamples_per_code + excluded_range_1) elif excluded_range_2 >= subsamples_per_code: code_phase_range = np.arange(excluded_range_2 - subsamples_per_code, excluded_range_1) else: code_phase_range = np.concatenate(( np.arange(0, excluded_range_1 - 1), np.arange(excluded_range_2, subsamples_per_code) )) print 'code_phase_range', code_phase_range.shape, code_phase_range assert code_shift not in code_phase_range second_peak_value = all_results[frequency_shift, code_phase_range].max() print 'Second peak value = %f' % second_peak_value peak_ratio = peak_value / float(second_peak_value) print 'peak_ratio = %f' % peak_ratio output['peak_ratios'][idx] = peak_ratio if peak_ratio > settings['acquisition_threshold']: output['found'][idx] = True print '* FOUND' else: output['found'][idx] = False print '* NOT FOUND' return output
def main(): # (a) Input code = ca_code.generate(prn=1, repeats=10) # rx = np.roll(ca_code.generate(prn=1, repeats=10), -10230/6*1) | ca_code.generate(prn=2, repeats=10) rx = np.roll(ca_code.generate(prn=1, repeats=10), 1000) | ca_code.generate( prn=2, repeats=10) noise_std = 1 noise = np.random.normal( scale=noise_std, size=code.size, ) rx = np.add(rx, noise) # (b) Aliasing q = 2 code_aliased = sfft_aliasing.execute(code, q) rx_aliased = sfft_aliasing.execute(rx, q) # (c) FFT code_f = fft(code_aliased) rx_f = fft(rx_aliased) # (d) Multiply code_rx_f = np.multiply(np.conjugate(code_f), rx_f) # (e) IFFT code_rx = np.real(ifft(code_rx_f)) print 'arg max is', np.argmax(code_rx) plt.figure('Local C/A code') plt.step(np.arange(code.size), code) plt.xlim(0, code.size - 1) plt.title('Local C/A code') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Received signal') plt.plot(rx) plt.xlim(0, code.size - 1) plt.title('Received signal') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Aliased Local C/A code') plt.step(np.arange(code_aliased.size), code_aliased) plt.xlim(0, code_aliased.size - 1) plt.title('Local C/A code (Aliased)') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Aliased Received signal') plt.plot(rx_aliased) plt.xlim(0, rx_aliased.size - 1) plt.title('Received signal (Aliased)') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Local CA code') plt.plot(np.abs(fftshift(code_f))) plt.title('FFT of Local C/A code') plt.xlabel('Frequency') plt.xlim(1000, 4000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Received signal') plt.plot(np.abs(fftshift(rx_f))) plt.title('FFT of Received signal') plt.xlabel('Frequency') plt.xlim(1000, 4000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Multiply') plt.plot(np.abs(fftshift(code_rx_f))) plt.title('Multiply') plt.xlabel('Frequency') plt.xlim(1500, 3500) plt.ylim(0, 100000) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('IFFT') plt.plot(code_rx) plt.title('IFFT') plt.xlim(0, code_rx.size - 1) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.show()
def main(): # (a) Input code = ca_code.generate(prn=1, repeats=10) rx = np.roll(ca_code.generate(prn=1, repeats=10), 10230 / 4 * 3) | ca_code.generate(prn=2, repeats=10) noise_std = 1 noise = np.random.normal( scale=noise_std, size=code.size, ) rx = np.add(rx, noise) # (b) FFT code_f = fft(code, n=code.size) rx_f = fft(rx) # (c) Multiply code_rx_f = np.multiply(np.conjugate(code_f), rx_f) # (d) IFFT code_rx = np.real(ifft(code_rx_f)) plt.figure('Local C/A code') plt.step(np.arange(code.size), code) plt.xlim(0, code.size - 1) plt.title('Local C/A code') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Received signal') plt.plot(rx) plt.xlim(0, code.size - 1) plt.title('Received signal') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Local CA code') plt.plot(np.abs(fftshift(code_f))) plt.title('FFT of Local C/A code') plt.xlabel('Frequency') plt.xlim(2000, 8000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Received signal') plt.plot(np.abs(fftshift(rx_f))) plt.title('FFT of Received signal') plt.xlabel('Frequency') plt.xlim(2000, 8000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Multiply') plt.plot(np.abs(fftshift(code_rx_f))) plt.title('Multiply') plt.xlabel('Frequency') plt.xlim(3000, 7000) plt.ylim(0, 100000) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('IFFT') plt.plot(code_rx) plt.title('IFFT') plt.xlim(0, code_rx.size - 1) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.show()
def main(): # (a) Input code = ca_code.generate(prn=1, repeats=10) rx = np.roll(ca_code.generate(prn=1, repeats=10), 10230/4*3) | ca_code.generate(prn=2, repeats=10) noise_std = 1 noise = np.random.normal( scale=noise_std, size=code.size, ) rx = np.add(rx, noise) # (b) FFT code_f = fft(code, n=code.size) rx_f = fft(rx) # (c) Multiply code_rx_f = np.multiply(np.conjugate(code_f), rx_f) # (d) IFFT code_rx = np.real(ifft(code_rx_f)) plt.figure('Local C/A code') plt.step(np.arange(code.size), code) plt.xlim(0, code.size-1) plt.title('Local C/A code') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Received signal') plt.plot(rx) plt.xlim(0, code.size-1) plt.title('Received signal') plt.xlabel('Time') plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Local CA code') plt.plot(np.abs(fftshift(code_f))) plt.title('FFT of Local C/A code') plt.xlabel('Frequency') plt.xlim(2000, 8000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('FFT of Received signal') plt.plot(np.abs(fftshift(rx_f))) plt.title('FFT of Received signal') plt.xlabel('Frequency') plt.xlim(2000, 8000) plt.ylim(0, 500) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('Multiply') plt.plot(np.abs(fftshift(code_rx_f))) plt.title('Multiply') plt.xlabel('Frequency') plt.xlim(3000, 7000) plt.ylim(0, 100000) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.figure('IFFT') plt.plot(code_rx) plt.title('IFFT') plt.xlim(0, code_rx.size-1) plt.setp(plt.gca().get_xticklabels(), visible=False) plt.setp(plt.gca().get_yticklabels(), visible=False) plt.show()