[英]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. 我得到了一个录音阵列,我想在上面添加一些噪音,以便稍后可以在Simulink中将其删除,然后将原始声音与删除噪音的声音进行比较。
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. 我的问题是我对Matlab的语言/函数还很陌生,所以我陷入了一个for循环中,但我不了解它在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: 要做到这一点,我将在python中使用伪代码像这样逐行执行:
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.
我很想知道如何在Matlab中实现它。
Obs: (This is also me trying to update an oldcode from my teacher so It works in new Matlab for other students) Obs:(这也是我尝试从老师那里更新旧代码,因此它可以在新的Matlab中为其他学生使用)
I like Daniel's solution, however there are some edits needed: 我喜欢Daniel的解决方案,但是需要进行一些编辑:
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. 如果要将
[0.0142, 0.0142]
添加到每一行,请使用bsxfun
将操作广播到每一行。
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. 通常,使用MATLAB而不是python时,要记住两件事:1)for循环的开销相当大; 2)将数据不断追加到数组上的开销非常大,因为每次都必须重新分配数据因为所有数组元素都存储在连续内存中。 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. 在MATLAB中,这通常是矩阵运算,而不是for循环,因为每次循环都会更改
other_thing
的大小。
Just make a copy of voice and add the values: 只需复制语音并添加值即可:
new_audio = voice
new_audio(:, 3) = noise(1)
new_audio(:, 4) = noise(2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.