Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.5 kB
2
Indexable
Never
import numpy as np
import scipy.io.wavfile as wav

def read_audio_file(filename):
    sample_rate, audio_data = wav.read(filename)
    return sample_rate, audio_data

def detect_frequency(audio_segment, freq_high, freq_low, sample_rate):
    fft_data = np.fft.fft(audio_segment)
    frequencies = np.fft.fftfreq(len(fft_data), 1/sample_rate)
    freq_high_amp = np.max(np.abs(fft_data[(frequencies >= freq_high - 10) & (frequencies <= freq_high + 10)]))
    freq_low_amp = np.max(np.abs(fft_data[(frequencies >= freq_low - 10) & (frequencies <= freq_low + 10)]))
    if freq_high_amp > freq_low_amp:
        return '1'
    else:
        return '0'

def decode_audio(audio_data, bit_duration, sample_rate, freq_high, freq_low):
    bits = ""
    segment_size = int(bit_duration * sample_rate)
    for i in range(0, len(audio_data), segment_size):
        audio_segment = audio_data[i:i+segment_size]
        bit = detect_frequency(audio_segment, freq_high, freq_low, sample_rate)
        bits += bit
    return bits

# Example usage:
filename = "encoded_audio.wav"  # Path to the audio file
bit_duration = 0.1  # Duration of each bit in seconds (should match the encoding)
sample_rate, audio_data = read_audio_file(filename)
freq_high = 1000  # Frequency for '1' bit (should match the encoding)
freq_low = 500  # Frequency for '0' bit (should match the encoding)

decoded_data = decode_audio(audio_data, bit_duration, sample_rate, freq_high, freq_low)
print("Decoded binary data:", decoded_data)
Leave a Comment