Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- def load_iq_file(filename):
- """
- Load IQ data from a file (complex values).
- :param filename: Path to the IQ file
- :return: Array of complex IQ data
- """
- try:
- iq_data = np.fromfile(filename, dtype=np.complex64) # 32-bit complex data
- print(f"Loaded {len(iq_data)} IQ samples.")
- return iq_data
- except Exception as e:
- print(f"Error loading file: {e}")
- return None
- def get_amplitude(iq_data):
- """
- Calculate the amplitude of the IQ data.
- :param iq_data: Array of complex IQ samples
- :return: Array of amplitude values
- """
- return np.abs(iq_data)
- def detect_morse_pulses(amplitude, threshold=0.5):
- """
- Detect Morse pulses (ON/OFF states) based on a threshold.
- :param amplitude: Array of amplitude values
- :param threshold: Amplitude threshold for signal detection
- :return: List of pulse durations (in samples)
- """
- is_signal_on = amplitude > threshold
- pulse_durations = []
- current_pulse = 0
- for state in is_signal_on:
- if state: # Signal is ON
- current_pulse += 1
- elif current_pulse > 0: # Signal turns OFF
- pulse_durations.append(current_pulse)
- current_pulse = 0
- print(f"Detected {len(pulse_durations)} pulses.")
- return pulse_durations
- def classify_pulses(pulse_durations, dot_length):
- """
- Classify pulse durations as dots, dashes, or spaces.
- :param pulse_durations: List of pulse durations (in samples)
- :param dot_length: Duration of a dot in samples
- :return: Morse code string
- """
- morse_code = []
- for duration in pulse_durations:
- if duration <= 1.5 * dot_length: # Dot
- morse_code.append("·")
- elif duration <= 3.5 * dot_length: # Dash
- morse_code.append("−")
- else: # Long pause (space between words)
- morse_code.append(" ")
- return "".join(morse_code)
- def morse_to_text(morse_code):
- """
- Convert Morse code to plain text.
- :param morse_code: String of Morse code
- :return: Decoded text
- """
- MORSE_DICT = {
- '·−': 'A', '−···': 'B', '−·−·': 'C', '−··': 'D', '·': 'E',
- '··−·': 'F', '−−·': 'G', '····': 'H', '··': 'I', '·−−−': 'J',
- '−·−': 'K', '·−··': 'L', '−−': 'M', '−·': 'N', '−−−': 'O',
- '·−−·': 'P', '−−·−': 'Q', '·−·': 'R', '···': 'S', '−': 'T',
- '··−': 'U', '···−': 'V', '·−−': 'W', '−··−': 'X', '−·−−': 'Y',
- '−−··': 'Z', '·−−−−': '1', '··−−−': '2', '···−−': '3',
- '····−': '4', '·····': '5', '−····': '6', '−−···': '7',
- '−−−··': '8', '−−−−·': '9', '−−−−−': '0'
- }
- words = morse_code.split(" ")
- decoded_message = []
- for word in words:
- letters = word.split(" ")
- decoded_word = ''.join(MORSE_DICT.get(letter, '?') for letter in letters)
- decoded_message.append(decoded_word)
- return ' '.join(decoded_message)
- # Example usage
- if __name__ == "__main__":
- # Path to your IQ file
- filename = "signal.iq" # Replace with your IQ data file path
- iq_data = load_iq_file(filename)
- if iq_data is not None:
- amplitude = get_amplitude(iq_data)
- # Plot the amplitude for visualization
- plt.figure(figsize=(10, 6))
- plt.plot(amplitude[:1000], label="Amplitude")
- plt.title("Amplitude of IQ Signal")
- plt.xlabel("Sample")
- plt.ylabel("Amplitude")
- plt.legend()
- plt.grid()
- plt.show()
- # Detect Morse pulses
- pulse_durations = detect_morse_pulses(amplitude, threshold=0.5)
- # Classify pulses into Morse code
- morse_code = classify_pulses(pulse_durations, dot_length=100) # Adjust `dot_length` as needed
- print(f"Decoded Morse Code: {morse_code}")
- # Convert Morse code to plain text
- text = morse_to_text(morse_code)
- print(f"Decoded Text: {text}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement