error: Subscripted assignment dimension mismatch
显示 更早的评论
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
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.
zozo
2012-1-4
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!