using nested for loop with 2D array

조회 수: 2 (최근 30일)
David Horowitz
David Horowitz 2017년 1월 26일
편집: per isakson 2017년 1월 26일
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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by