using nested for loop with 2D array

10 views (last 30 days)
David Horowitz
David Horowitz on 26 Jan 2017
Edited: per isakson on 26 Jan 2017
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 Comment
per isakson
per isakson on 26 Jan 2017
Edited: per isakson on 26 Jan 2017
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));

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!