Arrays have incompatible sizes for this operation. Error in CDproydosverdos (line 28) Rx_sample = bsxfun(@times, x(t + lags), x(t)); Related documentation
1 次查看(过去 30 天)
显示 更早的评论
% Define parameters
fc = 0.002; % Sinusoidal signal frequency (Hz)
T = 1000; % Duration of the signal (seconds)
N0 = 2; % Noise power spectral density (W/Hz)
A = sqrt(2); % Amplitude of the sinusoidal signal
M = 500; % Number of realizations for ensemble average
% Generate time vector
t = linspace(0, T, 10001);
% Calculate autocorrelation function components
Rx_sin = A^2 * cos(2*pi*fc*t);
Rx_noise = N0/2 * dirac(t);
% Initialize autocorrelation function and time lags
Rx = zeros(1, length(t));
lags = -T/2:T/2;
% Loop through realizations and time lags
for m = 1:M
% Generate random phase for each realization
theta = pi*(2*rand() - 1);
% Generate noisy sinusoidal signal
x = A*cos(2*pi*fc*t + theta) + sqrt(N0)*randn(size(t));
% Calculate autocorrelation for current realization
Rx_sample = bsxfun(@times, x(t + lags), x(t));%%%%%%%%%%%%%%%ERROR
% Accumulate average autocorrelation
Rx = Rx + Rx_sample/M;
end
% Plot autocorrelation function
figure;
plot(lags, Rx);
xlabel('Time lag (seconds)');
ylabel('Autocorrelation');
title('Autocorrelation of Sinusoidal Signal + White Gaussian Noise');
% Add theoretical curve for comparison
hold on;
plot(lags, Rx_sin + Rx_noise, 'r--');
legend('Calculated Autocorrelation', 'Theoretical Autocorrelation');
0 个评论
回答(1 个)
Walter Roberson
2023-12-17
t = linspace(0, T, 10001);
%...
x = A*cos(2*pi*fc*t + theta) + sqrt(N0)*randn(size(t));
x becames a 1 x 10001
T = 1000; % Duration of the signal (seconds)
lags = -T/2:T/2;
lags becomes -500:500 which is 1 x 1001
Rx_sample = bsxfun(@times, x(t + lags), x(t));%%%%%%%%%%%%%%%ERROR
t is 1 x 10001, logs is 1 x 1001 . You cannot add those two together.
If you could add them together (perhaps generating a 10001 x 1001 array) then the values would range from -500 to 1500, which would not be valid indices for x: indices for x must be positive integers.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!