using nested for loop with 2D array

2 次查看(过去 30 天)
Hello, I am working on a basic signals problem:
I have values for w(omega), phi(phase shift), and k(amplitude) stored in 3 separate 2D arrays. Each array is 131 row x 15 columns. To create a cosine I start at (1,1) in each array and plug those values into the formula for a cosine. Then I take that cosine I just created and add it to the cosine created from (1,2), then (1,3) and so on until I have summed the cosines from all 15 columns in that row. Once the row is complete I move onto the next row and do (2,1), (2,2), (2,3).... until I reach column 15. Once I have the sums of the 131 rows, I would like to add them together to make one long wave. The sampling frequency(fs) is 44.1kHz so each of the 131 rows should be 50ms in length when played separately and the whole thing should be about 6.5 seconds.
Here is the code I have created so far:
clear
%load data
load('data1.mat');
%find the magnitude of each cosine in the matrix
k = 2*abs(A);
%assign the frequencies in the matrix freqs to omega
w = 2*pi*freqs;
%find phi for each cell of A
phi = angle(A) ;
%define t
t = (1:2205)/fs;
%intialize the sum of the cosines
wave = 0;
for x=1:131
for y = 1:15
wave = wave + k(x,y)*cos(w(x,y)*t + phi(x,y));
end
end
sound(wave);
This isn't happening so I'm seeking your input. Thank you for your time.
  1 个评论
per isakson
per isakson 2017-1-26
编辑:per isakson 2017-1-26
TL;NR, however, in this assignment the scalar, wave, is overwritten in each iteration of the loop
wave = wave + k(x,y)*cos(w(x,y)*t + phi(x,y));

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by