+===================================================================+ | | | Relevant Matlab routines to generate simple auditory stimuli. | | | | John van Opstal Dec 1, 2003 | | | +===================================================================+ Install the following routines (http://www.mbfys.kun.nl/~johnvo/cnpa04/coll3 ) on your hard disk in your own MatLab directory Mymatlab. (Here, I suppose that Matlab has been installed on your computer under c:\matlabr11 ) Add the Mymatlab directory (e.g. c:\matlabr11\Mymatlab) to the default Matlab path with the command 'path' or 'addpath': e.g.: path(path, 'c:\matlabr11\mymatlab'); or: addpath c:\matlabr11\mymatlab (Note that either line can be added to the startup.m file that is found in the c:\matlabr11\work directory ) Also ensure that the SIGNAL TOOLBOX is installed. (some of the data filtering routines, like FIR2, FILTFILT, and PSD, are from this toolbox). ============================================================================================= [1] gengwn.m call: noise = GENGWN (N, NEnvelope, order, RMS) = GENGWN (N, NEnvelope) = GENGWN (N) Generates Gaussian White Noise (GWN) of N points (= N/50 msec). This is by default a mono signal! The signal is LowPass filtered at 22.5 kHz; The Nyquist frequency is at 25 kHz (sampling frequency = 50 kHz, which is taken default). The noise signal has on-offset ramps of NEnvelope samples (default NEnvelope = 250 = 5 ms), and a given LowPass filter order (default = 100, which is a steep filter characteristic). The default RMS-value of the noise (in [0.0 - 1.0]) is taken as 0.5 (1.0 is the maximum value allowed for the the sound card). The first 50 ms of the signal are shown in figure 1. If you need GWN on two channels there are several possibilities: [a] correlated noise in both channels: noise=[noise, noise]; both ears get identical inputs. [b] uncorrelated noise in both channels: generate gengwn twice (noise1 and noise2), and then: noise=[noise1, noise2]; puts noise1 in one channel, and noise2 in the other. [c] correlated noise in both channels with different sound levels: use db2amp to set the relative sound levels for each the two channels [d] correlated noise in both channels with a delay. Examples: noise = gengwn(20000); generates an array of 20000 GWN samples, which corresponds to a noise sound of 20000/50 = 400 ms with a bandwidt [0-22.5 kHz]. The signal has default 5 ms on- and offset ramps. noise = gengwn(200000, 500); generates an array of 200000 samples (4.0 s), with 10 ms smoothing on- and offset ramps. ============================================================================================= [2] lowpass.m A custom made routine to generate low-pass filtered noise. call: LP = LOWPASS (SIGNAL, FCUTOFF, NORDER) DESCRIPTION Lowpass filtering of time sequence stored in SIGNAL. INPUT PARAMETERS Signal - Time sequence FCutoff - Cutoff frequency (relative to Nyquist frequency) NOrder - Order of filter (defualt = 100) OUTPUT PARAMETER LP - Time sequence of low-pass filtered version of Signal. Example: suppose you first generated noise = gengwn(7500); (a 150 ms noise burst, with 5 ms on- and offset ramps) then: lp = lowpass(noise, 0.06); gives a low-pass filtered version of noise at 1.5 kHz (which is 0.06*25 kHz) lp2 = lowpass(noise, 0.15); gives low-pass filtered noise with a bandwidth up to 3.75 kHz. ============================================================================================= [3] highpass.m A custom made routine to generate high-pass filtered noise. call: HP = HIGHPASS (SIGNAL, FCUTOFF, NORDER) DESCRIPTION Highpass filtering of time sequence stored in SIGNAL. INPUT PARAMETERS Signal - Time sequence FCutoff - Cutoff frequency (relative to Nyquist frequency) NOrder - Order of filter (defualt = 100) OUTPUT PARAMETER HP - Time sequence of low-pass filtered version of Signal. Example: suppose you first generated noise = gengwn(7500); (a 150 ms noise burst, LP till 22.5 kHz, with 5 ms on- and offset ramps) then: hp = lowpass(noise, 0.12); gives a high-pass filtered version of noise at 3.0 kHz (which is 0.12*25 kHz) hp2 = lowpass(noise, 0.02); gives high-pass filtered noise with a bandwidth from [0.5 - 22.5] kHz. ============================================================================================= [4] gentone.m call: stim = gentone(freq, duration, t_envelope) Generates a pure tone with frequency freq (in Hz) and a duration that is given in ms. Also the enevelope duration should be given in ms (default: 5 ms). examples: tone1000 = gentone(1000, 200); a 1000 Hz tone of 200 ms with 5 ms on- and offset ramps tone4500 = gentone(4500, 2000, 100); a 4500 Hz tone of 2 s, with 100 ms on- and offset ramps. ============================================================================================= [5] tone.m Alternative tone-generating program, in which also the tone sound level (in dB re. 80 dB) is a parameter. 80 dB corresponds to 1.0 for the sound card. A plot of the first 50 ms of signal is also shown. x=tone(freq,level,dur,fs) All parameters are optional, with defaults: freq frequency (1000 Hz) level level (70 dB re 80dB=1) dur duration (200 ms) fs sampling freq (50000 Hz) Env envelope duration (5 ms) ============================================================================================= [6] envelope.m SIG = ENVELOPE (SIG, NENVELOPE); Generates a signal with smooth (sin^2) on- and offset ramps of nenvelope samples (default: nenvelope = 250, which corresponds to 5 ms). ============================================================================================= [7] amp2db.m AMP2DB converts amplitude to decibels with optional reference level USAGE: db=amp2db(level) db=amp2db(level,ref) The 2 argument form adds the reference to the dB level after conversion. It is useful to adopt a standard reference level of 80 dB to represent an amplitude of 1.0 for MATLAB sound output purposes. ============================================================================================= [8] db2amp.m DB2AMP converts decibels to amplitude with optional reference level USAGE: amp=db2amp(level) amp=db2amp(level,ref) The 2 argument form subtracts the ref from the level prior to conversion. It is useful to adopt a standard reference level of 80 dB to represent an amplitude of 1.0 for MATLAB sound output purposes. ============================================================================================= [9] delay.m call: out=delay(in, Nsamp) output signal is delayed by Nsamp relative to the input signal. input signal should have 1 channel only. With a sample frequency of 50 kHz, each sample is 20 microseconds apart. The first 20 ms of both signals are plotted. ============================================================================================= [9] psd.m (a standard Matlab routine from the Signal Toolbox) call: psd(noise, 256, 50000, 256); This function generates a plot with the horizontal axis as linear frequency (up to the Nyquist frequency, which is 25.0 kHz), and power spectral magnitude (in dB). A useful routine to quickly check the spectrum of the signal. ============================================================================================= [10] playsound.m (uses standard Matlab routine soundsc, at 50 kHz sampling rate). call: playsound(signal) signal values should be in the [-1.0 <= signal <= +1.0] range. ============================================================================================= [11] soundwrite.m (uses standard Matlab routine wavwrite, at 50 kHz sampling rate). call: soundwrite(Signal, Filename) example: soundwrite(lp1, 'lp1.wav'); writes (stereo or mono) signal lp1 in file lp1.wav on the current directory. =============================================================================================