I was trying to remember the Matlab code to generate a tone. It is posted below so that I don’t forget it again. The way it works is Matlab connects to a NI data acquisition device, which, in this case, is the NI USB-6211. You can use other NI DAQs, but I did notice the analog output needs to have a high sample rate. I first tried to use a NI USB-6002, which has a maximum sample rate of 5 kS/s, and it would play tones but there was also some high-pitched interference. A sample rate of 20 kS/s seemed to play most tones without interference. Anyway, the NI DAQ is connected to a powered speaker. I used inexpensive computer speakers that are powered by USB.
% Global Variables
ao_rate = 100000; % Hz or S/s
ao_tone = 500; % Hz
ao_duration = 0.5; % s
% Connect Matlab to the NI DAQ
session_ao = daq.createSession(‘ni’);
session_ao.addAnalogOutputChannel(‘Dev4’, 0, ‘Voltage’); % Dev# will depend on your device
session_ao.Rate = ao_rate;
% Create the Tone
ao_array = 1*sin((ao_tone*ao_duration)*2*pi/(ao_rate*ao_duration):(ao_tone*ao_duration)*2*pi/(ao_rate*ao_duration):(ao_tone*ao_duration)*2*pi);
ao_array = ao_array’;
% Output the Tone
session_ao.queueOutputData(ao_array);
session_ao.startBackground; % Alternatively, session_ao.startForeground will pause Matlab while the tone plays