Clear Filters
Clear Filters

i want convert recorded audio file to binary.

42 views (last 30 days)
hey, Im doing project in communication system . In which i have to transmit audio and then recieve the audio , so i want to convert the audio file to binary and then at reciever side i need to convert binary to audio.so i need matlab code for the same

Accepted Answer

Hassaan
Hassaan on 23 Feb 2024
% --- Audio to Binary ---
[audio, fs] = audioread('your_audio_file.wav'); % Load your audio file here
audio_normalized = int16(audio * 32767); % Normalize
audio_binary = dec2bin(typecast(audio_normalized(:), 'uint16'), 16); % Convert to binary
binary_vector = audio_binary(:)'; % Reshape to vector for transmission
% --- Binary to Audio ---
binary_matrix = reshape(binary_vector, [], 16); % Reshape back to matrix
audio_integers = bin2dec(binary_matrix); % Convert binary to decimal
audio_reconstructed = typecast(uint16(audio_integers), 'int16'); % Typecast to int16
audio_reconstructed_normalized = double(audio_reconstructed) / 32767; % Normalize to [-1, 1]
% Save the reconstructed audio
audiowrite('reconstructed_audio.wav', audio_reconstructed_normalized, fs);
% --- Optional: Play Original and Reconstructed Audio for Comparison ---
sound(audio, fs); % Play original audio
pause(length(audio)/fs + 1); % Wait for audio to finish plus a little extra
sound(audio_reconstructed_normalized, fs); % Play reconstructed audio
Notes
  • Assumes the audio is mono for simplicity. If your audio is stereo, you will need to handle both channels separately or convert it to mono.
  • The normalization step converts audio samples to the range of 16-bit integers. This is because dec2bin works on integers. If your audio samples use a different bit depth, adjust the normalization accordingly.
  • Ensure that the audio file path 'your_audio_file.wav' is correctly specified and that the output file 'reconstructed_audio.wav' is being saved to a location you have write access to.
  • Directly converts the entire audio file to binary and back. For an actual communication system, you would need to consider the transmission method, error handling, and possibly encoding/decoding techniques to ensure reliable communication.
  1 Comment
Walter Roberson
Walter Roberson on 23 Feb 2024
[audio_normalized, fs] = audioread('your_audio_file.wav', 'native'); % Load your audio file here
and
audio_reconstructed_normalized = typecast(uint16(audio_integers), 'int16'); % Typecast to int16
The audio functions are willing to work directly on int16

Sign in to comment.

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!