From e91f12ba01867bd3a8c07f3def2294bc9dc3c540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Pleva=C4=8D?= Date: Sun, 15 Oct 2023 15:35:22 +0200 Subject: [PATCH] Added dc part remove --- station/tools/baseband_spectogram.py | 16 ++++- station/tools/cw_morse.py | 92 +++++++++++++++++++--------- 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/station/tools/baseband_spectogram.py b/station/tools/baseband_spectogram.py index 58b7e67..bd838a8 100644 --- a/station/tools/baseband_spectogram.py +++ b/station/tools/baseband_spectogram.py @@ -11,7 +11,7 @@ if __name__ == '__main__': cliParser.add_argument('input_file', type=str, help='input filename') cliParser.add_argument('output_file', type=str, help='output filename') cliParser.add_argument('-fs', '--sampleRate', type=float, help='sets the sample rate [hz]') - cliParser.add_argument('-fc', '--centralFreq', type=float, help='sets the sample rate [hz]') + cliParser.add_argument('-fc', '--centralFreq', type=float, help='sets center freq of spectrum [hz]') cliParser.add_argument('-f', '--format', type=str, help='Output format', @@ -30,10 +30,21 @@ if __name__ == '__main__': num_rows = len(data) // fft_size // sampleSize # ok compute how many data to one row - num_rows_real = 100 + num_rows_real = 1024 if num_rows < num_rows_real: num_rows_real = num_rows + # find DC part + DC_PART = 0 + for i in range(num_rows): + subdata_start = i * sampleSize * fft_size + subdata = data[subdata_start : subdata_start + fft_size * sampleSize] + + subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + DC_PART += np.sum(subdata) + + DC_PART /= num_rows * fft_size + abstract_rows_per_row = int(num_rows / num_rows_real) spectrogram = np.zeros((num_rows_real, fft_size)) @@ -45,6 +56,7 @@ if __name__ == '__main__': subdata = data[subdata_start : subdata_start + fft_size * sampleSize] subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + sub_fft -= DC_PART # remove DC part cur_fft = 10 * np.log10(np.abs(np.fft.fftshift(np.fft.fft(subdata)))**2) diff --git a/station/tools/cw_morse.py b/station/tools/cw_morse.py index 704ae2b..198bda9 100644 --- a/station/tools/cw_morse.py +++ b/station/tools/cw_morse.py @@ -1,11 +1,12 @@ #!/usr/bin/env python -# Simple CW morse decoder +# Simple spectogram ploter import argparse import numpy as np +import matplotlib.pyplot as plt if __name__ == '__main__': - cliParser = argparse.ArgumentParser(description='Simple CW morse decoder') + cliParser = argparse.ArgumentParser(description='Simple spectogram ploter') cliParser.add_argument('input_file', type=str, help='input filename baseband record') cliParser.add_argument('output_file', type=str, help='output filename decoded text') @@ -34,42 +35,65 @@ if __name__ == '__main__': failChar = args.failChar downSample = int(sample_rate // down_rate) + sampleSize = 2 - data = data[1::2] + 1j * data[0::2] - data = data[1::downSample] # downsample - data = data - np.mean(data) # normalize + num_rows = len(data) // (fft_size * sampleSize * downSample) + num_samples = len(data) // (sampleSize * downSample) - num_rows = len(data) // fft_size - - beep_trashold = [] - + DC_PART = 0 for i in range(num_rows): - fft = np.abs(np.fft.fftshift(np.fft.fft(data[i*fft_size:(i+1)*fft_size]))) - frame_max = np.max(fft[len(fft)//2 - bandwidth//2 + offset_freq : len(fft)//2 + bandwidth//2 + offset_freq]) - beep_trashold.append(frame_max) + subdata_start = i * sampleSize * fft_size * downSample + subdata = data[subdata_start : subdata_start + fft_size * sampleSize * downSample] + + subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + subdata = subdata[0::downSample] # downsample + DC_PART += np.sum(subdata) - beep_trashold = np.mean(beep_trashold) + DC_PART /= num_samples + + print(f"DC part is {DC_PART}") + + beep_trashold = 0 - beep = np.zeros(num_rows) - for i in range(num_rows): - fft = np.abs(np.fft.fftshift(np.fft.fft(data[i*fft_size:(i+1)*fft_size]))) + subdata_start = i * sampleSize * fft_size * downSample + subdata = data[subdata_start : subdata_start + fft_size * sampleSize * downSample] + + subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + subdata = subdata[0::downSample] # downsample + subdata -= DC_PART + + fft = np.abs(np.fft.fftshift(np.fft.fft(subdata))) frame_max = np.max(fft[len(fft)//2 - bandwidth//2 + offset_freq : len(fft)//2 + bandwidth//2 + offset_freq]) - if (frame_max >= beep_trashold): - beep[i] = 1 - + beep_trashold += frame_max + + beep_trashold /= num_rows + + print(f"beep trashold is {beep_trashold}") + + is_one = False mean_one_len = [] mean_zero_len = [] sig_len = 0 - for signal in beep: - if signal == 1 and not(is_one): + for i in range(num_rows): + subdata_start = i * sampleSize * fft_size * downSample + subdata = data[subdata_start : subdata_start + fft_size * sampleSize * downSample] + + subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + subdata = subdata[0::downSample] # downsample + subdata -= DC_PART + + fft = np.abs(np.fft.fftshift(np.fft.fft(subdata))) + frame_max = np.max(fft[len(fft)//2 - bandwidth//2 + offset_freq : len(fft)//2 + bandwidth//2 + offset_freq]) + + if (frame_max >= beep_trashold) and not(is_one): mean_zero_len.append(sig_len) sig_len = 0 is_one = True - - if signal == 0 and is_one: + + if (frame_max < beep_trashold) and is_one: mean_one_len.append(sig_len) sig_len = 0 is_one = False @@ -82,9 +106,19 @@ if __name__ == '__main__': is_one = False morse = "" sig_len = 0 - - for signal in beep: - if signal == 1 and not(is_one): + + for i in range(num_rows): + subdata_start = i * sampleSize * fft_size * downSample + subdata = data[subdata_start : subdata_start + fft_size * sampleSize * downSample] + + subdata = subdata[1::2] + 1j * subdata[0::2] # convert to complex + subdata = subdata[0::downSample] # downsample + subdata -= DC_PART + + fft = np.abs(np.fft.fftshift(np.fft.fft(subdata))) + frame_max = np.max(fft[len(fft)//2 - bandwidth//2 + offset_freq : len(fft)//2 + bandwidth//2 + offset_freq]) + + if (frame_max >= beep_trashold) and not(is_one): is_one = True if sig_len >= mean_zero_len * 2: morse += "\n" @@ -92,15 +126,15 @@ if __name__ == '__main__': morse += " " sig_len = 0 - - if signal == 0 and is_one: + + if (frame_max < beep_trashold) and is_one: is_one = False if sig_len > mean_one_len: morse += "-" else: morse += "." sig_len = 0 - + sig_len += 1