error: Subscripted assignment dimension mismatch

1 次查看(过去 30 天)
I have this function to delay signals in freq. domain:
function [sd, tn] = df(s,fs,d,ns)
% Get signal processing parameters:
% Find length and deminsion of input signal
[r,c] = size(s);
% Covert to column vecotors for processing
if r == 1;
s = s.'; % Ensure it is a column vector
[rn,c] = size(s);
elseif c == 1
[rn,c] = size(s); % Otherwise rn = r indicating it came in as a column vector
else % If multi column and row then process each column as its own signal
[rn,c] = size(s); % Otherwise input was columnwise matrix
if length(d) ~= c
error('Number of columns in signal matrix must equal number of elements in delay vector')
end
end
if min(d) < 0;
error('Delay cannot be negative')
end
% Compute requested delay in sample points rounded up
nd = ceil(d*fs);
% Determine final output length
if nargin == 4
slen = ceil(ns*fs);
else
slen = max(nd)+rn; % Take max of all delays in vector to determin final signal length
end
sd = zeros(slen,c);
if slen <= rn;
sd = s(1:slen,:);
else
sd(1:rn,:) = s;
end
% Create frequency shift vector in the frequency domain
nfft = 2^nextpow2(2*slen);
fax = fs*(-nfft/2:nfft/2-1)'/nfft;
% Loop through each column of signal matrix and apply delay
for k=1:c
shft = exp(-j*d(k)*2*pi*fax); % Frequency function for delay
shft = ifftshift(shft); % Make axis compatable with numeric FFT
fsd = fft(sd(:,k),nfft); % Take FFT
fsd = fsd.*shft; % Apply delay
dum = ifft(fsd); % Return to time domain
sd(:,k) = dum(1:slen); % Trim time domain signal to required length
end
% Restore dimension of signal vector to original orientation
if rn == r % Was already a column vector or multiple signal vector
if nargout == 2 % Create time axis if requested
tn = (0:slen-1)' /fs;
end
else % If it was originally a column vector, transpose
sd = sd.';
if nargout == 2 % Create time axis if requested
tn = (0:slen-1)/fs;
end
end
% If original signal was real, make output real
if isreal(s)
sd = real(sd);
end
It works well for all delays individually. But when I enter them in a loop, I get this error:
Subscripted assignment dimension mismatch.
Here is my program:
fs=100; %sampling freq
f1=20; % freq in Hz
f2=30; % freq in Hz
t_duration = 4; % 4 seconds
t = 0:1/fs:t_duration-1/fs;
sn=sin(2*pi*f1*t)+randn(size(t));
Time_delay=[0 0.5576 0.2302 0.2398 0.2593 0.3073 0.2166 0.1245];
out=zeros(size(sn));
for d = 1:numel(Time_delay)
out(d,:)= df(sn,fs,Time_delay(d));
end
disp(out)
I want the output (out 8x402) with each row having the signal delayed by the values specified in Time_delay.
Please help.
  2 个评论
David Young
David Young 2012-1-4
Your code calls delayf() but the function you display is df(). Please edit your question to show the function that you actually call.

请先登录,再进行评论。

采纳的回答

David Young
David Young 2012-1-4
The problem is that the length of the output vector depends on the time delay. Each time df is called, it produces a vector of a different length (400 the first time, 456 the second time ...). These vectors can't be made the rows of a rectangular matrix.
I can suggest two possible solutions.
If you want each vector to have its full length, you can store them as the elements of a cell array. To do this, replace the line inside the loop with
out{d} = df(sn,fs,Time_delay(d));
and also remove the call to zeros which initialises out (or replace it with the appropriate call to cell).
The second possibility is to use the fourth argument to df to set the length of the output vector. Then they will all be the same length. This argument appears to specify the output length in seconds, rather than as a number of samples, but you could modify the code to set the length explicitly.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by