measure frequency response of an audio device -凯发k8网页登录
the frequency response (fr) is an important tool for characterizing the fidelity of an audio device or component.
this example requires an audio device capable of recording and playing audio and an appropriate audio driver. to learn more about how the example records and plays audio data, see and .
description of fr measurement techniques
an fr measurement compares the output levels of an audio device to known input levels. a basic fr measurement consists of two or three test tones: mid, high, and low.
in this example you perform an audible range fr measurement by sweeping a sine wave from the lowest frequency in the range to the highest. a flat response indicates an audio device that responds equally to all frequencies.
setup experiment
in this example, you measure the fr by playing an audio signal through audiodevicewriter
and then recording the signal through audiodevicereader
. a loopback cable is used to physically connect the audio-out port of the sound card to its audio-in port.
audio device reader and writer
to start, use the audiodevicereader
system object™ and audiodevicewriter
system object to connect to the audio device. this example uses a focusrite scarlett 2i2 audio device with a 48 khz sampling rate.
samplerate = 48e3; device = "focusrite usb asio"; adr = audiodevicereader( ... samplerate=samplerate, ... device=device, ... driver="asio", ... bitdepth="16-bit integer", ... channelmappingsource="property", ... channelmapping=1); adw = audiodevicewriter( ... samplerate=samplerate, ... device=device, ... driver="asio", ... bitdepth="16-bit integer", ... channelmappingsource="property", ... channelmapping=1);
test signal
the test signal is a sine wave with 1024 samples per frame and an initial frequency of 0 hz. the frequency is increased in 50 hz increments to sweep the audible range.
samplesperframe = 1024; sinesource = audiooscillator( ... frequency=0, ... signaltype="sine", ... samplerate=samplerate, ... samplesperframe=samplesperframe);
spectrum analyzer
use the spectrumanalyzer
to visualize the fr of your audio i/o system. 20 averages of the spectrum estimate are used throughout the experiment and the resolution bandwidth is set to 50 hz. the sampling frequency is set to 48 khz.
rbw = 50; navg = 20; scope = spectrumanalyzer( ... samplerate=samplerate, ... rbwsource="property",rbw=rbw, ... averagingmethod="exponential", ... forgettingfactor=0, ... frequencyspan="start-and-stop-frequencies",... startfrequency=0, ... stopfrequency=samplerate/2, ... plotastwosidedspectrum=false, ... frequencyscale="log", ... plotmaxholdtrace=true, ... showlegend=true, ... ylimits=[-110 20],... ylabel="power", ... title="audio device frequency response");
frequency response measurement loop
to avoid the impact of setup time on the fr measurement, prerun your audio loop for 5 seconds.
once the actual fr measurement starts, sweep the test signal through the audible frequency range. use the spectrum analyzer to visualize the fr.
tic while toc < 5 x = sinesource(); adw(x); y = adr(); scope(y); end count = 1; readerdrops = 0; writerdrops = 0; while true if count == navg newfreq = sinesource.frequency rbw; if newfreq > samplerate/2 break end sinesource.frequency = newfreq; count = 1; end x = sinesource(); writerunderruns = adw(x); [y,readeroverruns] = adr(); readerdrops = readerdrops readeroverruns; writerdrops = writerdrops writerunderruns; scope(y); count = count 1; end release(adr) release(adw) release(scope)
frequency response measurement results
the spectrum analyzer shows two plots. the first plot is the spectrum estimate of the last recorded data. the second plot is the maximum power the spectrum analyzer computed for each frequency bin, as the sine wave swept over the spectrum. to get the maximum hold plot data and the frequency vector, you can use the object function getspectrumdata
and plot the maximum hold trace only.
data = getspectrumdata(scope); freqvector = data.frequencyvector{1}; freqresponse = data.maxholdtrace{1}; semilogx(freqvector,freqresponse); xlabel("frequency (hz)"); ylabel("power (dbm)"); title("audio device frequency response");
the frequency response plot indicates that the audio device tested in this example has a flat frequency response in the audible range.