what do the AudioDeviceWriter do with a complex inputdata?

30 views (last 30 days)
康
on 15 Sep 2025 at 14:30
Commented: William Rose on 19 Sep 2025 at 19:21
when i use ifft() to process and get a frame of audio data but forget to real() the results, it sounds weired.
so i wonder what happened when AudioDeviceWriter play with a complex inputdata?
i try it with a 1kHz pure tone and then plus it with a very little imag part,when played independently(commented the other),these two sound very different. but when i play the two in sequence with a pause(3),when the complex one first and real one latter,they sound the same(weired).
furthermore. when i change the order, put the real one first and the complex one latter, an error occured.
i'll be glad to get an answer to the question ,thank you!
here is the code,you can change the order of adw(playbuff2) and adw(playbuff) then test ;
clc;
clear;
fs=48e3;
f0=1e3;
T=1;
N=1:T*fs;
t=N/fs;
wav=sin(2*pi*f0*t).';
adw=audioDeviceWriter('SampleRate',fs);
wav2=wav;
wav2=wav+1e-18*j*ones(size(wav));
playbuff2=[wav2];
playbuff=[wav];
adw(playbuff2);
pause(3);
adw(playbuff);

Answers (2)

William Rose
William Rose on 15 Sep 2025 at 19:35
I ran your code and got the same results you describe:
No error if I play the complex file, then the real. Both playbacks sound normal.
If I play the real file, then the complex, th real file sounds normal (but louder than before), and there is an error when the script tries to play the complex file.
Error using ()
Changing the complexity (from real to complex) on input 1 of System object
audioDeviceWriter is not allowed without first calling the release() method.
Error in complexSoundTest (line 18)
adw(playbuff2);
The error message explains why: it is not allowed to change the data type from real to complex unless you release the audio device writer.
Why do you want to play a complex array as a sound file? A sound recording (in the time domain) is not complex.
  5 Comments
Jimmy Lapierre
Jimmy Lapierre on 18 Sep 2025 at 15:42
May I recommend ifft's 'symmetric' flag, that's one way to avoid the small complex residual causing issues (the output will always be real). Just make sure there's not a bug in the code causing the data to not be conjugate symmetric.
William Rose
William Rose on 19 Sep 2025 at 19:21
@康, I agree with @Jimmy Lapierre that you may want to examine your code carefully for possible errors. The signal complexSoundRecordedBySoundCard, which you uploaded, suggests that mistakes may have occurred. I say this because the original signal power spectrum plot which you shared (copied below) looks odd: it looks like the two-sided spectrum of a signal sampled at 24 kHz, instead of the one-sided spectrum of a signal sampled at 48 kHz.

Sign in to comment.


Jimmy Lapierre
Jimmy Lapierre on 16 Sep 2025 at 15:23
This object should probably error out for complex values (which is not possible with any audio device). Instead, it looks like it's interpreting the buffer's complex values as samples to play, like this:
playbuff3=zeros(96e3,1);
playbuff3(1:2:end,:)=real(playbuff2);
playbuff3(2:2:end,:)=imag(playbuff2);
adw(playbuff3(1:48e3,:))
And as William said, you might need to release the object to change data type (or buffer length, etc.).
  1 Comment
Walter Roberson
Walter Roberson on 16 Sep 2025 at 16:05
Note that complex numbers started to be stored in interweaved format as of R2017b. Before that, real and imaginary portions were stored seperately.

Sign in to comment.

Categories

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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!