0

I want to play all frequencies given in matrix(FrTm) with its duration.The actual duration is one second but each frequency has to play for 3 of 1/18 and 6 of 1/18 seocnd such as given in matrix(FrTm).

 function Music()
Fs=44100;
T=1/Fs;
M = zeros(1,88);
for I=7:88,
M(I) = round(36.8*(2^(1/12))^(I-6));
end
Signal=[];

FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];

t=0:1/18:1;

for i=1:length(FrTm),
M(i)=FrTm(i);
Z=M(i);

data= sin(2*pi*Z/Fs*t);

signal=[data;signal];
end

stem(Signal);
sound (Signal, 44100);

end
  • I don't really understand what you to do. `FrTm` is a 59x2 matrix, with the 1st column the frequency and the 2nd telling whether it should be 3/18 or 6/18 of a second long when you play it? But then you say the total duration is 1 second? Can you please elaborate more on what you want to achieve? Also, sometimes `signal` has a capital letter, sometimes not, and, assuming you want 1 large vector for `signal` it should be `signal=[data signal]`. – ViG Jan 26 '18 at 13:35
  • ViG first of all thanks for comment.Ok assume any duration but some frequencies has to be played is mentioned 3/18 of duration and some 6/18 as given in FrTm matrix.Sorry Signal start with capital S now let's help me out what ever you can do to generate guitar sound from this.That's what i did so far. – Tayyab Khan Shinwari Jan 26 '18 at 17:50
  • ok i changed program.new code only create figure doesn't play why? t=0:1/18:5; for i=1:length(FrTm), M(i)=FrTm(i,1); Z=M(i); data= sin(2*pi*Z/Fs*t); Signal=[data;Signal]; end stem(Signal); sound(Signal, 44100); end – Tayyab Khan Shinwari Jan 27 '18 at 06:30
  • I'm still not sure what you mean with 3/18 of the total length, because you 1st want 50 Hz played for 3/18 of the total length, then again, then 52 Hz for 3/18, then 54 and 50 and 54, then you have 18/18. So what's with all the others then? The sum of the 2nd column of FrTm is 192, which means that you would have 192/18 times the length you want? I know have something that obeys the to total duration but each frequency is played for 3/192 or 6/192 of the total duration. Maybe the reason you can't hear it is because of the low amplitude – ViG Jan 27 '18 at 10:52
  • ok plz make some changes so that i can hear guitar chords of these frequencies. – Tayyab Khan Shinwari Jan 27 '18 at 15:34
  • I'll post what I got, then we can edit it till it is what you want. – ViG Jan 27 '18 at 15:44

3 Answers3

0

The classical way to make a sound with a given frequency (f) and sample frequency (Fs) is to make a time vector with step 1/Fs:

time = 0:1/Fs:D;

Where Dis the duration of the signal. The signal itself is then:

signal = sin(2*pi*f.*time)

In this case the total time is fixed, not the time of each signal. The total time is denoted with T, and the total time vector is made as

time = 0:1/Fs:T;

The sum of the second column is the total number of units the vector time needs to be divided in, e.g. 50, 3 means that a signal at 50 Hz needs to be played for 3 units. This means we only need a time vector of the length of 3 units:

t = time(1:floor(end*duration/s));

Where duration is the number of units for this part and s is the total number of units. The signal is then simply, as stated above,

data = sin(2*pi*f*t);

The data is then appended to the whole signal. The complete code, looks like this:

Fs = 44100; % sample frequency [Hz]
T = 3; % total duration [s]
time = 0:1/Fs:T;

% matrix with frequencies and duration
FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];

s = sum(FrTm(:,2));

[m, ~] = size(FrTm);
signal = [];

for i=1:m
    freq = FrTm(i,1);
    duration = FrTm(i,2);

    t = time(1:floor(end*duration/s));

    data = 10*sin(2*pi*freq.*t);
    signal = [data signal];
end

stem(signal);
sound(signal, 44100);

Note instead of declaring time in the beginning, it is possible to make a new vector each time you run through the loop. In that case omit time = 0:1/Fs:T; and change t = time(1:floor(end*duration/s)); to t = 0:1/Fs:floor(end*duration/s);

ViG
  • 1,848
  • 1
  • 11
  • 13
  • can you explain the following code you wrote. s = sum(FrTm(:,2)); time = 0:1/Fs:T; [m, ~] = size(FrTm); signal = []; A = zeros(1,m); for i=1:m freq = FrTm(i,1); duration = FrTm(i,2); t = time(1:floor(end*duration/192)); – Tayyab Khan Shinwari Jan 27 '18 at 16:10
  • the sound is so unpleasant but thank you so much it least i hear it.one last demand can you make it a bit pleasant – Tayyab Khan Shinwari Jan 27 '18 at 16:13
  • ok forget about all i am posting code you just complete for loop part – Tayyab Khan Shinwari Jan 27 '18 at 16:19
  • @TayyabKhanShinwari I've added some explanation. Let me now if something is unclear. It's unpleasant because of either the frequencies, maybe do `10*freq`, that would make it better. In that case you can omit the `10*` in front of the sine. Or because the total duration is too short, then just choose a larger value for `T`. – ViG Jan 27 '18 at 16:42
  • thank you so much i will experiment something if i can reach to possible sound i have heard of this code.One thing plz let me know if you solved my below code i will be very thank full. – Tayyab Khan Shinwari Jan 27 '18 at 17:27
  • @TayyabKhanShinwari Am I only allowed to work in the for-loop? What's the use of the 1st for loop? – ViG Jan 27 '18 at 17:46
  • Yes you are only allowed to work in for loop.The truth is for as i know it do rounding of each index but i am new to matlab .I don't know much about it what it exactly its doing – Tayyab Khan Shinwari Jan 27 '18 at 18:37
  • @TayyabKhanShinwari I know it rounds something, but you never use that matrix again, so why going throught the trouble calculating it? – ViG Jan 27 '18 at 19:10
  • actually it was a bit homework that has to be fill line of codes after for loop it was about using karplus strong algorthim to generate guitar chords so i did what i could. Actually what is to write code after for loop sorry to disturb you i know i have asked more than what i deserve and you did 2000% so thank you so much. – Tayyab Khan Shinwari Jan 27 '18 at 19:24
  • @TayyabKhanShinwari I've added the code to your answer, but it will only be visible after it is peer reviewed – ViG Jan 27 '18 at 19:26
  • i have not received yet where it has been? i don't see in comment nor in answer – Tayyab Khan Shinwari Jan 27 '18 at 19:45
  • @TayyabKhanShinwari it is currently being [peer reviewed](https://stackoverflow.com/review/suggested-edits/18643926) – ViG Jan 27 '18 at 19:46
  • Thank you so much now i will change accordingly thank you :-* – Tayyab Khan Shinwari Jan 27 '18 at 19:49
0
 function Music()
Fs=44100;
T=1/Fs;
M = zeros(1,88);
for I=7:88,
M(I) = round(36.8*(2^(1/12))^(I-6));
end
Signal=[];

FrTm=[50 3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];

for i=1:length(FrTm),
%---------------------------------------------------
% complete the function

freq = FrTm(i,1);
duration = FrTm(i,2);
time =0:1/Fs:1;  % change the 1 to change total duration

s = sum(FrTm(:,2));
t = time(1:floor(end*duration/s));


data = sin(2*pi*freq.*t);
Signal = [data Signal];

end

stem(Signal);
sound (Signal, 44100);

end
ViG
  • 1,848
  • 1
  • 11
  • 13
0

This is the exact code what i wanted ViG can you please remove this tak tak sound it just a noise actually how to use envelope function to remove thid tak tak sound in music code is following.

Fs=44100;
  T=1/Fs;
  M=zeros(1,88);
  for I=7:88
    M(I)=round(36.8*(2^(1/12))^(I-6));
    end
    signal=[];
    FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
    49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
    50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
    45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
    50,6];
    for i=1:length(FrTm)
      x=FrTm(i,1);
      y=FrTm(i,2);
      F=M(x);
      time=0:1/Fs:y/18;
      sinewave=sin(2*pi*F*time);
      signal=[signal sinewave];
     end
     stem(signal)
     sound(signal,Fs)