Jose Garcia
Jose Garcia

Reputation: 19

need help creating a vector and using it to play my playnote function

i have created a playNote Function with 1 input and no output,

i want to create a vector of four frequencies 330, 440, 554, 659 but i don't know how to

then i want to test the playNote Function with the vector of four frequencies that is created

%%
function playFreq(x)
switch x
    case 1 
        Freq = 659;
    case 2
        Freq = 554;
    case 3
        Freq = 440;
    case 4
        Freq = 330;
    case 5
        Freq = 220;
end
    Fs = 8000; % speed
    ts = 1./Fs;
    t = [0:ts:0.5]; % 0.5 sec
    y = sin(Freq.* 2.* pi.* t);
    sound(y,Fs);
    pause(.5);

end

Upvotes: 0

Views: 42

Answers (1)

R.Falque
R.Falque

Reputation: 944

If I understand properly, you want to play several frequencies at the same time.

You can rewrite your function to take a vector as the input:

function playFreq(Freq)
    Fs = 8000; % speed
    ts = 1./Fs;
    t = 0:ts:0.5; % 0.5 sec
    y = zeros(size(t));
    for i = 1:length(Freq)
        y = y + sin(Freq(i).* 2.* pi.* t);
    end
    y = y/max(y(:));
    sound(y,Fs);
    pause(.5);
end

Then you can call your function by defining a vector of frequency:

Freq = [330, 440, 554, 659];
playFreq(Freq);

Upvotes: 1

Related Questions