I want to add random impulse noise to an audio signal, But the dimensions of input audio and noise audio both are different and that's why they are not getting added.

8 次查看(过去 30 天)
I am doing a project to remove impulse noise from audio sample using median filter. For this first I have to add the impulse noise to the signal and to do that I am using rand() to genrate random impulses of random amplitude and then trying to add them to the orignal signal but while adding I am getting error that
"
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in class_03 (line 25)
noise_y2 = [y, y_2];
"
clc;
clear;
close all;
[y, Fs] = audioread("teste_01.wav");
t = y(500:200000); % taking smalle sample from 'y' to add noise
N = round(length(t));
%%
y_2 = 0.25.*randn(1, N); % creating random noise
% trying to make array 'y_2' of same dimension as of y
y_dim = size(N);
scaled_data_dim = size(y_2);
if y_dim(1) < scaled_data_dim(1)
y_2 = repmat(y_2, y_dim(1), 1);
end
if y_dim(1) > scaled_data_dim(1)
noisy_padding = [y, zeros(1, scaled_data_dim(1) - y_dim(1) )];
y_2 = [y_2 ; noisy_padding];
end
%%
% adding both orignal and impulse noise array
noise_y2 = [y, y_2];
figure
subplot(2, 1, 1)
plot(y)
title('Orignal audio signal')
subplot(2, 1, 2)
plot(noise_y2)
title('audio with impulse noise')

采纳的回答

Walter Roberson
Walter Roberson 2023-11-23
[y, Fs] = audioread("teste_01.wav");
That will return a 2D array, with the columns being channels (one channel would still be a column array.)
t = y(500:200000); % taking smalle sample from 'y' to add noise
N = round(length(t));
If y was a column array (one channel) then t will end up being a column array, but if y had multiple columns (multiple channels) then t will end up being a row vector. (Using one index parameter on a vector gives a result that is a vector the same orientation as the original vector, but using one index parameter on something that is not a vector gives a result that is the same shape as the index.)
Note that if the file has multiple channels but fewer than 200000 rows then you are using linear indexing on it and would in that case be extracting information from multiple columns. If you want to extract 200000 samples per channel, use two indexing parameters, y(500:200000,:) .
N is going to be 200000 - 500 + 1 = 199501
y_2 = 0.25.*randn(1, N); % creating random noise
That is a row vector. Remember that t will be a column vector if y has one channel but a row vector if y has multiple channels.
y_dim = size(N);
N was calculated using length(t) which is always going to return a scalar integer. round() of the scalar integer leaves it as a scalar integer. size() of the scalar integer is going to be 1 x 1
The rest of the code is suspect because it is based upon the size of the scalar, rather than the size of y or t.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Transmitters and Receivers 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by