# Playing time-locked square wave using sound() function

5 ビュー (過去 30 日間)
YCR 2021 年 3 月 8 日

Hello,
I'm a graduate student interested in playing a 1 KHz square wave, at a 50% duty cycle, as a sound mask for a type of stimulation that human participants will receive. This mask will need to last for 0.7 seconds.
I have found examples of how to use the square() function to generate the wave and even graph it, but I was wondering how it may be used with sound() to generate the noise directly from the computer? My other option would be to create an external trigger for a waveform signal generator, but I feel like generating the noise strictly from MATLAB is possible.
Any suggestions?
Thanks.

サインインしてコメントする。

### 採用された回答

Star Strider 2021 年 3 月 8 日
Try this:
Fs = 1E4;
t = linspace(0, 0.7, 0.7*Fs);
s = ((1+sign(sin(2*pi*t*1E3)))/2).'*[1 1]; % Create Binaural Signal
sound(s, Fs)
figure
subplot(2,1,1)
plot(t, s)
grid
subplot(2,1,2)
plot(t, s)
grid
xlim([0 0.07])
.
##### 2 件のコメントなしを表示なしを非表示
YCR 2021 年 3 月 8 日
Yes this works! Thank you so much.
Star Strider 2021 年 3 月 8 日
As always, my pleasure!

サインインしてコメントする。

### その他の回答 (2 件)

Gabriele Bunkheila 2021 年 3 月 26 日
Apologies for the late response. If you haven't done so yet, I recommend taking a look at audioOscillator and possibly its joint use with audioDeviceWriter. You can find some code to get started in this seemingly unrelated example.
One of the advantages of audioOscillator over other (still valid) legacy signal-generation functions pointed out in previous answers is that it enables generating the signal samples live, while allowing to tune parameters like duty cycle or tone frequency during execution (for example, via user interfaces). I don't understand that is a requirement for you here, but I thought I'd point that out for future visitors.
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

サインインしてコメントする。

Walter Roberson 2021 年 3 月 8 日
You can use sound() as Star Strider shows, but the timing on it is terrible for the purpose of stimulation studies.
AudioPlayer offers more control over timing, but it is still weak for the purpose of stimulation studies.
Audio System Toolbox offers finer control than either of those.
However... for stimulation studies, I recommend that you look at Psychtoolbox, a third-party product specifically designed for psychophysical experiments. It has visual and audio facilities, and tries hard to be precise, such as having a number of different internal strategies to work with different hardware setups to provide frame-accurate timings.
##### 2 件のコメントなしを表示なしを非表示
YCR 2021 年 3 月 8 日
Thank you for the suggestion. The problem is that the stimulation is controlled by a custom matlab script which triggers the stimulation hardware to stimulate for ~500 ms per trial. This also signals ANOTHER program (Signal 6.05) to initiate another stimulation hardware type. I envisioned that I can incorporate the sound() function to run 100 ms before each trial. This is also why it isn't possible to use the waveform generator triggered by the stimulation script, as sound-masking needs to begin before stimulation.
I'll look into the audio toolboxes you've suggested and I'll probably have to run some testing with the hardware to see how precise each option will be.
Walter Roberson 2021 年 3 月 8 日
sound() is one of the worst choices for accurate timings. It constructs internal audio mechanisms and runs them whenever it is ready. It returns to the command line after it has started playing, before it has finished, but you cannot query the state.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeAudio I/O and Waveform Generation についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by