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
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
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!