Hey all,
Slowly getting there with my program but would like some advice on the best method to preform the sine wave generation. So far it works but all waves play at the same time and i want each one to play for 1 sec in turn. Obviously my code for this is so far clumsy and long so i'd need to iterate through something to reduce my code and to get it so do the desired thing..
Below is my full code, just so you can see where some of the data and variables come from:
prompt={'1st Note:','2nd Note:','3rd Note:','4th Note:','5th Note:','6th Note:','7th Note:'};
name='Enter Note Values (C-B)..';
defaultans={'C','D','E','F','G','A','B'};
answer=inputdlg(prompt,name,[1,50],defaultans);
% These three lines replace all of your loop and elseif statements:
V = 'CDEFGAB';
idx = cellfun(@(c)find(V==c),answer);
frq = [261.626,293.665,329.628,349.228,391.995,440.000,493.883];
out = frq(idx);
% GENERATE SINE WAVES..
%samples
N = T*Fs;
Fs = 44100;
%samples vector
tX1 = 0:1/Fs:1;
tX2 = 1:1/Fs:2;
tX3 = 2:1/Fs:3;
tX4 = 3:1/Fs:4;
tX5 = 4:1/Fs:5;
tX6 = 5:1/Fs:6;
tX7 = 6:1/Fs:7;
% CREATE SINE WAVES..
Sine1 = sin(2*pi*out(1)*tX1);
Sine2 = sin(2*pi*out(2)*tX2);
Sine3 = sin(2*pi*out(3)*tX3);
Sine4 = sin(2*pi*out(4)*tX4);
Sine5 = sin(2*pi*out(5)*tX5);
Sine6 = sin(2*pi*out(6)*tX6);
Sine7 = sin(2*pi*out(7)*tX7);
% PLAY SOUNDS..
soundsc(Sine1,Fs)
soundsc(Sine2,Fs)
soundsc(Sine3,Fs)
soundsc(Sine4,Fs)
soundsc(Sine5,Fs)
soundsc(Sine6,Fs)
soundsc(Sine7,Fs)
Thanks,
Paul..

3 commentaires

I've tried this as a loop but its not quite right:
% GENERATE SINE WAVES..
%samples
N = T*Fs;
Fs = 44100;
tXstart=0;
f = out(1);
for i = 1:7;
tX = tXstart:1/Fs:tXstart+i;
f = out(i+i);
SineZ{i} = sin(2*pi*f*tX);
soundsc(SineZ{i}, Fs);
end
It plays some horrible sounds then some of them as expected but not the full 7 second loop as intended. I'm getting closer though haha
N/A
N/A le 12 Fév 2019
why doesnt this generates a graph of the wave?
Star Strider
Star Strider le 12 Fév 2019
@Maaz Rao —
Do you see a plot call in that code?
I don’t.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 17 Nov 2015

0 votes

I didn’t run your code, but if you want to create an audioplayer object, you can use the playblocking function.
Otherwise, it’s likely easier to insert a pause(n) call (where ‘n’ is the number of seconds each tone lasts) after your soundsc call.

8 commentaires

Hey Star, cool thanks. I'll look into play and playblocking or even add the pause as you suggest. I'll let you know and tick this answer once i've got it sorted :)
My pleasure. I’ve used both methods in my code successfully.
Star, I for speed i used the pause idea and with adapting some of my code as i spotted a few mistakes, it now works as i need it. Good shout, thanks :)
As always, my pleasure!
I know this should be a new question but as its related I thought I'd throw it out there haha:
Using the code now as:
for i = 1:7;
tX = 0:1/Fs:1;
%tX = tXstart:1/Fs:tXstart+i;
f = out(i);
SineZ{i} = sin(2*pi*f*tX);
soundsc(SineZ{i}, Fs);
pause(1);
end
This may sound dumb but how can i combine all the elements of SineZ into another variable so i can plot the entire 7 second sine melody?
I tried:
plot(tX,SineZ)
but it errors on the SineZ obviously. I want to do the same thing to tX.
I want to have something thats like when you:
[y,Fs] = audioread('some.wav');
plot(t,y)
but using my user created melody, not an audioread. So i can plot the entire melody in one graph
I took a few liberties with your code here to make it work:
frq = [261.626,293.665,329.628,349.228,391.995,440.000,493.883];
Fs = 44100;
T = 1;
for i = 1:7;
tX = 0:1/Fs:1;
%tX = tXstart:1/Fs:tXstart+i;
f = frq(i);
SineZ{i} = sin(2*pi*f*tX);
soundsc(SineZ{i}, Fs);
pause(1);
end
SineZ_p = cell2mat(SineZ); % Concatenate Vectors
t_p = linspace(0, length(SineZ_p), length(SineZ_p)); % Create Matching Time Vector
sound(SineZ_p, Fs); % Check To Be Sure The Concatenation Worked
figure(1)
plot(t_p, SineZ_p)
grid
The figure is not very informative, since you’re plotting 308707 data.
The sound call after the t_p assignment is not necessary for the code, and can be deleted. I kept it in because it offers an alternative way of playing your entire ‘SineZ’ vector without having to use any pause calls.
That's awesome. I need to work on the plots now properly but the main sound generation bit is done. Thanks so much for your help, Star :)
As always, my pleasure!
I learn a lot from solving problems here, so it’s a win for all.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by