简体   繁体   中英

Matlab - Iterating through an array and appending to a new one

I got an array that is a audio recording, and I'd like to add some noise on it so later I can remove it in Simulink and compare the original to the one that I removed the noise.

My problem is that I'm pretty new to Matlab's languages/functions, so I got stucked in a for loop that I didnt understand how It works properly in Matlab.

I got this huge array (voice recorded):

voice = [0.0012    0.0012;
         0.0003    0.0005;
         (....)    (....);]

And what I'd like to do is to add some values to each line, so it will be noisy (another array):

noise = [0.0142    0.0143]

To do It I would do line by line in python with a pseudo code like this:

new_audio = []
for line in voice:
    new_line = (line+noise)
    new_audio.append(new_line)

I need to keep the original so I can compare later. Could you guys give me a hand on it? I'd love to know how to make it happen in Matlab.

Obs: (This is also me trying to update an oldcode from my teacher so It works in new Matlab for other students)

I like Daniel's solution, however there are some edits needed:

new_audio = voice;
new_audio(:,1) = new_audio(:,1) + noise(1);
new_audio(:,2) = new_audio(:,2) + noise(2);

Here is an alternative way to accomplish the same end:

voice = [0.0012, 0.0012;0.0003, 0.0005; 0.0025, 0.0100];
noise = [0.0142,0.0142];
dim1Size = size(voice,1);
dim2Size = size(voice,2);
voiceWithNoise = zeros(dim1Size,dim2Size);
for dim1Idx = 1:dim1Size
   voiceWithNoise(dim1Idx,:) = voice(dim1Idx,:)+noise; 
end

What you want to do is just create a new array from the old one and add the noise. If you want to add [0.0142, 0.0142] to every row, then use bsxfun to broadcast the operation to each row.

noisy = bsxfun(@plus, voice, [0.0142, 0.0142]);

What I think that you actually want though is different noise for each sample of your data. To do this, then just create your matrix of noise and add it to your original data.

% Create some random noise
noise = rand(size(voice)) - 0.5;

% Add this to your original signal
noisy = voice + noise;

In general, there are two things to remember when working with MATLAB as opposed to python: 1) for loops tend to be pretty costly and 2) constantly appending data to an array is very costly because the data has to be re-allocated every time since all array elements are stored in contiguous memory. So if you find yourself doing something like:

for thing in things:
    other_thing.append(thing)

In MATLAB this would typically be a matrix operation rather than a for loop that changes the size of other_thing with each iteration.

Just make a copy of voice and add the values:

new_audio = voice
new_audio(:, 3) = noise(1)
new_audio(:, 4) = noise(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM