Playing Music in Matlab - matlab

I want to access the frequencies one by one from the list of frequencies(Frtm) and want to store them in a signal and at the end i want to play them ...
I have written this code but i don't know where is the problem if anyone one can help then it will be appreciated.
function Music3 ()
Fs = 44100;
T = 1 / Fs;
t = 0:1 / 18:5;
M = zeros (1, 88);
for I = 7:88
M (I) = round (36.8 * (2 ^ (1 / 12)) ^ (I - 6));
endfor
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)
M (i) = FrTm (i);
New = M (i);
data = sin (2 * pi * New / Fs * t);
Signal = [data; Signal];
endfor
stem(Signal);
sound(Signal,44100);
end

Here is what you want:
clear; clc; close all;
Fs = 44100;
T = 1 / Fs;
sin_time_seconds = 1
t = 0:T:sin_time_seconds;
Signal = [];
FrTm = 10*[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)
data = sin (2 * pi .* FrTm(i) .* t);
Signal = [Signal data];
end
plot(Signal);
sound(Signal,Fs);
I multiplied the frequencies by 10 else they were too low.
sin_time_seconds is the time you want each sound to be displayed. There were many errors in your script, especially with the dimensions of the vectors you created.

Related

Sort 2 arrays/vectors based off 1 vector

I have 2 arrays (vectors? in m vernacular?) and I want to sort them in unison. How can I achieve this in Matlab?
For example; I have found the peaks from a histogram and they are stored in 2 arrays; peakXVals, peakYVals. They will always be arranged in ascending x axis index. So they will always look like:
peakXVals = [0, 3, 20, 77, 240];
peakYVals = [10, 999, 30, 40, 20];
I wish to sort both arrays based of the values in peakYVals in descending order. Ie from largest peak to smallest peak. So the desired result is:
peakXVals = [3, 77, 20, 240, 0];
peakYVals = [999, 40, 30, 20, 10];
What function's can I use to achieve this in Matlab?
Use sort:
peakXVals = [0, 3, 20, 77, 240];
peakYVals = [10, 999, 30, 40, 20];
>> [B,I] = sort(peakYVals, 'descend')
B =
999 40 30 20 10
I =
2 4 3 5 1
Then:
>> peakXVals_sorted = peakXVals(I)
peakXVals_sorted =
3 77 20 240 0
>> peakYVals_sorted = B
peakYVals_sorted =
999 40 30 20 10
You can arrange the two vectors as columns of a matrix and sort the rows of that matrix as atoms, in lexicographical order. Then the results are the columns of the sorted matrix:
tmp = sortrows([peakYVals(:) peakXVals(:)], 'descend');
peakYVals = tmp(:,1).';
peakXVals = tmp(:,2).';

How to display multiple variables of matrix and array using a single command in MATLAB?

a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
b = [1, 2, 3; 4, 5, 6; 7, 8, 9];
c = zeros(3,1);`
d = rand([4 1],'single');
or
d = rand([4 1],'double');
e = transpose(b);
f = pinv(e);
g = e*e;
h = [0:100];
i = [0:100:1000];
Use display command or fprintf to display the above 9 variables.
You could add them into a temporary struct, which will then print its fields and their contents on screen:
struct('a', a, 'b', b, 'c', c, 'd', d, 'e', e, 'f', f, 'g', g, 'h', h, 'i', i)
a = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];
b = [1, 2, 3; 4, 5, 6; 7, 8, 9];
c = zeros(3,1);
d = rand([4 1],'single');
A = cell(4,1) ;
A{1} =a ;
A{2} = b ;
A{3} = c ;
A{4} = d ;

Using RSA encryption, how can i find d

I am using the euclidean algorithm when i have p = 163, q = 311 and e = 281. Here is what i have so far
N = p * q = 50693
Totient Symbol(n) = 162 x 310 = 50220
1. 50220 = 178(281) + 202
2. 281 = 1 (202) + 79
3. 202 = 2 (79) + 44
4. 79 = 1 (44) + 35
5. 44 = 1 (35) + 9
6. 35 = 3 (9) + 8
7. 9 = 1 (8) + 1
8. 8 = 8 (1) + 0
I then move on to back substitution
A. 9 = 1 (8) + 1 === 1 = 9-1(8)
B. 8 = 35 – 3(9)
C. 1 = 1(9)-1(35-3(9)) -
D. 1 = 3(9) – 1(35) + 1(9) add similar items
E. 1 = 4(9) -1(35)
9 = 44 – 1(35)
1 = 4 (44-1(35)) – 1(35)
1 = 4(44)-4(35)-1(35)
1 = 4(44) – 5(35)
Take value next to 35 (5), subtract it from totient
50220 – 5 = 50215
d = 50215
This is wrong as i used an online calc to verify . Can anyone point me in the right direction here, i think the back substituion is wrong
There are two different ways to calculate RSA d values, the φ (phi / totient) method, and the λ (lambda / least common multiple) method. While the original RSA paper (and RFC 2313) use phi, modern implementations (and RFC 2437) use lambda.
The totient value is easy: (p-1)(q-1) = 50220.
For lambda(p-1, q-1) we need to first compute GCD(p-1, q-1), the example uses the subtraction form of the Euclidian algorithm:
GCD(162, 310)
GCD(162, 148)
GCD(14, 148)
GCD(14, 134)
GCD(14, 120)
GCD(14, 106)
GCD(14, 92)
GCD(14, 78)
GCD(14, 64)
GCD(14, 50)
GCD(14, 36)
GCD(14, 22)
GCD(14, 8)
GCD(6, 8)
GCD(6, 2)
GCD(4, 2)
GCD(2, 2)
GCD = 2
The least common multiple of (a, b) is a * b / GCD(a, b). So the lambda value is the totient / GCD, or 25110.
Now, to compute dPhi = ModInv(e, phi) or dLambda = ModInv(e, lambda) we can use the Extended Euclidean Algorithm:
ModInverse(281, 50220)
r=50220, newR=281, t= 0, newT= 1
r= 281, newR=202, t= 1, newT= -178
r= 202, newR= 79, t= -178, newT= 179
r= 79, newR= 44, t= 179, newT= -536
r= 44, newR= 35, t= -536, newT= 715
r= 35, newR= 9, t= 715, newT=-1251
r= 9, newR= 8, t=-1251, newT= 4468
r= 8, newR= 1, t= 4468, newT=-5719
r= 1, newR= 0, t=-5719, newT=50220
Correcting the sign of t
dPhi = 44501
ModInverse(281, 25110)
r=25110, newR=281, t= 0, newT= 1
r= 281, newR=101, t= 1, newT= -89
r= 101, newR= 79, t= -89, newT= 179
r= 79, newR= 22, t= 179, newT= -268
r= 22, newR= 13, t= -268, newT= 983
r= 13, newR= 9, t= 983, newT=-1251
r= 9, newR= 4, t=-1251, newT= 2234
r= 4, newR= 1, t= 2234, newT=-5719
r= 1, newR= 0, t=-5719, newT=25110
Correcting the sign of t
dLambda = 19391
You seem to have done the descending step of the Extended Euclidean Algorithm correctly, but being unfamiliar with the back-propogation calculation (as opposed to the inline form) I don't see where you made a value or arithmetic error.

I have a matrix 12*4, and I need to subtract the 3rd column elements of rows that are different

I have a 12x4 matrix in MATLAB,
A =[-1, 3, 152, 41.5 ;
3, 9, 152, 38.7 ;
9, 16, 152, 38.7 ;
16, 23, 129, 53.5 ;
23, 29, 129, 53.5 ;
29, 30, 100, 100 ;
30, 30.5, 83, 83 ;
30.5, 31, 83, 83 ;
31, 35, 83, 83 ;
35, 41, 129, 53.5 ;
41, 48, 129, 53.5 ;
48, 55, 152, 38.7 ] ;
and I need to find the changes in the rows by subtracting the 3rd column element of the 2nd row from the previous row 3rd column element if they are different else go to the 3rd row if the same.
The answer should be in the form:
B = [16, 23;
29, 29;
30, 17;
35, 46;
48, 23]
For example, the 3rd and the 4th row 3rd column elements are different, so if subtracted i got 23. Output B 1st column element will consist of the 4th row first column element.
%Given matrix
A =[-1, 3, 152, 41.5 ;
3, 9, 152, 38.7 ;
9, 16, 152, 38.7 ;
16, 23, 129, 53.5 ;
23, 29, 129, 53.5 ;
29, 30, 100, 100 ;
30, 30.5, 83, 83 ;
30.5, 31, 83, 83 ;
31, 35, 83, 83 ;
35, 41, 129, 53.5 ;
41, 48, 129, 53.5 ;
48, 55, 152, 38.7 ] ;
B=A(:,2:3); %Taking out the columns of our interest
B = B([diff(B(:,2))~=0; true],:); %Storing only those rows whose consecutive elements in the third column of A are different
B=[B(1:end-1,1) abs(diff(B(:,2)))] % First column is according to your condition and second column is the difference

replacing values of a matrix with an if operation using matlab

mn = 1
for kn = 1:199
for sn = 1:19773
if abs((x1c{kn+1,1}(sn)) - (x1c{kn,1}(sn))) >= 20
extract{mn} = x1c{kn+1,1}(sn);
mn = mn+1;
end
end
end
extend = cell2mat(extract) + 40;
How can I change the values of "x1c" with the values of "extend"?
You are performing the operation on a cell. Considering you're comparing numbers, this would be done far more efficiently when done with matrices.
I therefor suggest you convert the cell (or a subset of it) to a matrix and then use vectorized operations, like this:
>> a={[13, 2, 3], [14, 25, 8], [100, 9, 10], [101, 8, 32], [140, 20, 3]};
>>
>> x = transpose(reshape(cell2mat(a), 3, []));
>> z = abs(x(2:end, :) - x(1:end-1,:)) > 20;
>> z2 = [zeros(1,3); z]
z2 =
0 0 0
0 1 0
1 0 0
0 0 1
1 0 1
>> x(logical(z2)) = x(logical(z2)) - 200
x =
13 2 3
14 -175 8
-100 9 10
101 8 -168
-60 20 -197
There are two alternatives if you really must use cells (I don't recommend it for speed reasons).
store the indices (k, sn) of the cell items where your condition holds true. And then you'd have to loop over the elements again (very inefficient).
You'd store the previous and next cell "row" in temporary variables and compare using those. When the condition holds, edit in-place and take the temporary variable with you in the next iteration of the loop. The code below shows how this is done:
a={[13, 2, 3], [14, 25, 8], [100, 9, 10], [101, 8, 32], [140, 20, 3]};
curr_row = a{1};
for rowind=1:4
next_row = a{rowind+1};
for colind=1:3
if abs(next_row(1, colind) - curr_row(1, colind)) > 20
a{rowind+1}(1, colind) = a{rowind+1}(1, colind) + 40;
end
end
curr_row = next_row;
end