Store matrixes with variable dimensions to stereo WAVE files

4 次查看(过去 30 天)
Hello dear experts,
I want to write IQ data from radio to stereo wav-File (use WAVE like datacontainer). So I (real part) to one channel, and Q (imaginary part) to another channel.
I found an example code to make this with random sine waves:
fs = 8000; % frequency sample rate
i=1/fs; % interval
t = 0:i:2; % time
phi = 0;
A = 1;
% F = 220Hz
f = 220;
x = A*sin((2*pi*f*t) + phi); % 1x16001 double
% F = 240Hz
f = 240;
y = A*sin((2*pi*f*t) + phi); % 1x16001 double
stereo_mtx = [x(:), y(:)]; % join to 2x16001 double
audiowrite('stereo_sound.wav', stereo_mtx, fs); % write stereo wav-file
to get x and y data back, I just need to use:
xy = audioread('stereo_sound.wav');
x = xy(:,1)'; % 1x16001 double
y = xy(:,2)'; % 1x16001 double
--------------------------------------------------------------------------------------------------------------------------------
So, now, I want to do it with IQ matrix-data. My radio saves data as a matrixes with dimensions: framelength x frames = (example) 2048x1000 complex doubles (that I split to real ang imag parts).
Example code:
x2 = rand(2048,1000); % 2048x1000 double (real part)
y2 = rand(2048,1000); % 2048x1000 double (imag part)
stereo_mtx_2 = [x2(:), y2(:)]; % packing all rows after each other
audiowrite('stereo_sound2.wav', stereo_mtx_2, fs); % write stereo wav-file
now, I have a stereo file with my stored data.
When I try to read a data, using:
xy2 = audioread('stereo_sound2.wav'); % 2048000x2 double
x2 = xy2(:,1)'; % 1x2048000 double
y2 = xy2(:,2)'; % 1x2048000 double
I get back very long arrays. So, I have to reconstruct array to matrixes, using:
x2 = reshape(x2,[2048,1000]); % reconstruction from 1x2048000 double to 2048x1000 double
y2 = reshape(y2,[2048,1000]); % reconstruction from 1x2048000 double to 2048x1000 double
to get my matrixes with right dimensions and values again.
Now my questions:
1. How can I save information about frame length and frames in wav-file? If I send a wav-file with different framelengthes and frames to an algorithm, that should reconstruct a matrix data automatically (I know about 2048x1000 but not an algorithm). Maybe by special header?
2. While saving and reading data to/from WAVE—some data goes lose:
Example:
a = -0.010650959807123 % before audiowrite
a = -0.010650992393494 % after audioread
how can I improve a resolution? (WAVE, double, is already 64 Bits per sample?)
Thank you in advice
Best regards
Nik
  3 个评论
Jonas
Jonas 2022-10-5
converning the coding of frame length and number of frames: you could either write it to the file name and use information from the file name automatically when opening the files or you write the information to a text string into the meta tag of your files like Title, Artist or Comment
also note that .wav supports up to 1024 channels (not only stereo, 2), if you have your number of frames or fram elength is constantly smaller, you could abuse that
Nik Rocky
Nik Rocky 2022-10-5
编辑:Nik Rocky 2022-10-5
Thank you very much for both answers.
Okay, I see i can write file like:
audiowrite('stereo_sound2.wav', stereo_mtx_2, fs,'BitsPerSample',64); % write 64 bit wav-file
and about first answer - I like an Idea to write is to Comment and Title:
track2 = 'stereo_sound2.wav';
framelength = height(x2); % 2048
frames = width(x2); % 1000
audiowrite(track2, stereo_mtx_2, fs,'BitsPerSample',64,'Title',num2str(framelength),'Comment',num2str(frames)); % write stereo wav-file
than I can use:
info = audioinfo(track2); % read metadata
xy2 = audioread(track2) ; % read IQ data
framelength = str2num(info.Title); % extract framelength information from Title
frames = str2num(info.Comment); % extract frames information from Comment
x2 = xy2(:,1)'; % 1x2048000 double
y2 = xy2(:,2)'; % 1x2048000 double
x2 = reshape(x2,[framelength,frames]); % reconstruction from 1x2048000 double to 2048x1000 double
y2 = reshape(y2,[framelength,frames]); % reconstruction from 1x2048000 double to 2048x1000 double
So it should works!

请先登录,再进行评论。

回答(0 个)

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by