Home > OS >  Noise cancellation with fft failing - unable to assign elements
Noise cancellation with fft failing - unable to assign elements

Time:11-09

I have the following code for noise cancellation:

[z,fs] = audioread('noisy_voices.wav'); % Use >>soundsc(z, fs) to hear the unprocessed signal
zproc_vec=zeros(1,length(z));
tail = zeros(1,256);
for k = 0:128:length(z)-256
Z = fft(z(k  1:k   256).* hann(256));
[zmax, zl] = max(abs(Z(1:128)));
Z(zl-3: zl  3)=0;
Z(256-(zl-3:zl  3) 2)=0;
zproc = ifft(Z);
zproc = zproc tail;
tail(1:128) = zproc(129:256);
zproc_vec(k 1:k 256)=zproc;
end
soundsc(zproc_vec , fs)

Could anyone tell me why I get this error?

Unable to perform assignments because the left and right sides have a different number of elements

Error in task_one (line 12) zproc_vec(k 1:k 256)=zproc;

CodePudding user response:

I think the output of your Z = fft( ___ ) line will be a column vector, but you initialise tail to be a row vector with tail = zeros(1,256);

So on this line:

zproc = zproc tail;

Implicit expansion would make zproc a square 256*256 matrix.

Then your indexing fails, as specified by the error message, because you're trying to assign this square matrix to the 256 elements you're indexing with zproc_vec(k 1:k 256).

Initialising tail to a column vector should solve the issue.

Alternatively you could take the "lazy" way out and make sure you're only operating on column vectors to create zproc

zproc = zproc(:) tail(:); % Make both terms column vectors for addition

CodePudding user response:

''Unable to perform assignments because the left and right sides have a different number of elements''

What part of the error message do you not understand? It means that the variables on the left and the right side of the equal sign have different sizes so you can't assign the right thingy to the left thingy.

Check the sizes and make sure they are the same.

  • Related