How to generate sound in matlab from a wave?

15

If I create a sine wave:

  • x=0:0.1:6*pi; is the wave range
  • y=sin(x); is the wave with amplitude 1
  • f=440; is the desired frequency

How to generate a sound from this data?

I've tried sound(y, f) , but it does not work.

How to proceed?

    
asked by anonymous 21.09.2016 / 13:42

1 answer

11

There's so much missing, let's break it down!

x=0:0.1:6*pi;

I do not know what your intention was to do 6*pi , it does not make any sense, you must have confused things, calm that I already get there ...

Your x starts from 0 and linearly goes through values with 0.1 to 6*pi = 18.8496

To exemplify your x is:

0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000 ..... 18.800

This will give you a vector x size 189 , it seems to be a very short size to listen to anything and the tbm values do not reflect the periodicity you want ...

Your second line:

y=sin(x);

Yes it will generate a sine of size 189 and according to the values contained in x , but you already noticed that the values of your x is not exactly what you expected né ...

Third line:

f=440;

OK you want to generate a 440Hz sine

Fourth line:

sound(y, f)

Play your sine% y on the frequency of 440Hz do you think this is ?? humm is not the way it works ... In the sound function the first parameter really should be the values of the sine / sound / etc, the second parameter says to play the sound at the sampling rate at which your vector was created. >

Right way to do it:

Set how many sine-wave seconds you want, and set a sampling rate (44100, 8000, 2000) at any rate at which the theory of Nyquist do not disturb the human range of audible perception, something between 20hz até 20Khz in theory your sampling rate can be 924hz até 44100hz (yes 924Hz = f*2.1 pq you want to create a sinusoid at a frequency of 440Hz, anything below this sampling rate will impact the theorem and you will have problems, audio may start to blink or complete silence.)

But let's appeal and use a taxa de amostragem = 44100 in theory (in practice tbm) will cover any frequency up to 22050Hz or all our audible spectrum is within: -)

segundos=3
Fs=44100;
f=440;
tamanho=segundos*Fs
sinal=1*sin(2*pi*f/Fs*(1:tamanho)); % 1 * sin é a amp, poderia qualquer outro valor de amplitude só coloquei pra exemplificar
sound(sinal, Fs)

Notice the formula 2*pi*f/Fs and then it is multiplied by the vector that is linearly separated by 1 .

Then just use sound to listen.

    
04.11.2016 / 16:04