How do you generate dual frequencies in MATLAB?

I am interested in producing a tone frequency at runtime, with frequency and duration being variable parameters. What would be the best way to create this sound file in MATLAB and make it available in a script for later combining with other sound files generated in a similar way for different frequencies / durations? Thanks in advance for your comments.

+7
matlab audio
source share
2 answers

The duration this vector will play depends on the number of elements in the vector and the sampling frequency. For example, a 1000-element vector, when it is reproduced at a frequency of 1 kHz, will last 1 second. When playing at a frequency of 500 Hz, it lasts 2 seconds. Therefore, the first choice you must make is the sample rate you want to use. To avoid aliasing , the sampling rate should be twice as large as the largest frequency component of the signal . However, you can do this even more to avoid attenuation of frequencies close to the sample rate.

Given a sampling frequency of 1 kHz, the following example creates an audio vector of a given duration and tone frequency (using LINSPACE and SIN ):

Fs = 1000; %# Samples per second toneFreq = 50; %# Tone frequency, in Hertz nSeconds = 2; %# Duration of the sound y = sin(linspace(0, nSeconds*toneFreq*2*pi, round(nSeconds*Fs))); 

When playing back at 1 kHz using the SOUND function, this vector will generate a 50 Hz tone for 2 seconds:

 sound(y, Fs); %# Play sound at sampling rate Fs 

Then the vector can be saved as a wav file using the WAVWRITE function:

 wavwrite(y, Fs, 8, 'tone_50Hz.wav'); %# Save as an 8-bit, 1 kHz signal 

Now the sound vector can be loaded using the WAVREAD function. If you intend to combine two sound vectors, you must make sure that they are both designed to use the same sampling rate.

+19
source share

The gnovice code looks erroneous. I think it messed up the linspace function. You can verify this yourself by comparing the same frequency with different sampling frequencies - the sound is different, which is obviously not worth it.

(I'm stupid. When using the Matlab sound function, be sure to use the same sample rate that you used to generate the tone. Otherwise, you will have a bad time.)


Here is the function that completes the generation of tonality.

 function pureTone ( frequency, duration, amplitude, sampleFreq, save2file ) % Generate pure tones. % Enter at least 1 argument. % Defaults are: % duration 1 sec % amplitude 1 % sampleFreq 48000 Hz % save2file no %-------------------- % If you want to save the tone to a file, provide a name. switch nargin case 0 error('Enter a frequency.') case 1 duration = 1; amplitude = 1; sampleFreq = 48000; save2file = 0; case 2 amplitude = 1; sampleFreq = 48000; save2file = 0; case 3 sampleFreq = 48000; save2file = 0; case 4 save2file = 0; end t = linspace( 0, duration, duration * sampleFreq ); % http://de.wikipedia.org/wiki/Sinuston s = amplitude * sin( 2 * pi * frequency * t ); sound( s, sampleFreq ); if save2file wavwrite( s, sampleFreq, 32, save2file); end end 
-one
source share

All Articles