Index exceeds matrix dimensions.
조회 수: 4 (최근 30일)
이전 댓글 표시
i run the ADC&DAC simulations, and i getting this error :
Index exceeds matrix dimensions.
Error in ADC (line 80)
if(A(i)== j )
how can i solve this problem? i'm a newbie btw :S ^^'
here are the code :
%ADC & DAC
clear all;
close all;
clc;
fs=500000; % taking sampling frequency as 500kHz
fm=10000; % input signal frequency 10kHz
t=1:200; % displaying 200 samples
x=5*cos(2*pi*(fm/fs)*t); %input sinusoidal signal
z=awgn(x,1);% adding white Gaussian noise to the input signal with S/N=1
h=1:1000;
plot(t,x,'g','LineWidth',2); % plotting input signal
hold on;
plot(t,z,'r','linewidth',1.5); % plotting noisy signal
hold on;
stem(t,z);
hold on;
Vd=-5:0.0390625:5; % step size =0.0390625, when n=8 bits
for i=1:256
Vdelta(i)=(Vd(i)+Vd(i+1))/2; % Quantization levels
end
i=0:255;
binary= dec2bin(i); % decimal to binary conversion
% Quantization of input signal
for i=1:200
for j=1:256
if(z(i)< Vd(1))
z(i) = Vdelta(1);
end
if (z(i) > Vd(257))
z(i) = Vdelta(256);
end
if(z(i) <= Vd(j+1) && z(i) >= Vd(j))
z(i) = Vdelta(j);
end
end
end
% Encoding the Quantized data
for i=1:200
for j=1:256
if (z(i)==Vdelta(j))
B_data(i,1:8) = binary(j,1:8);
end
end
end% representing binary data in decimal
figure
for i=1:200
B(i)=bin2dec(B_data(i,1:8));
end
% First solution; writing Encoded data into ADC.txt file. The we will perfom
% Moving average filter operation in VHDL
f = fopen('ADC.txt', 'w');
for n = 1:200
fprintf(f, '%s\n', B_data(n,1:8));
end
fclose(f);
subplot(221);
plot(x);
title('original sinwave','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in volts');
subplot(222);
plot(z);
title('noise signal','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in volts');
% After the moving avg filter the filtered data has been written to vhdl_out.txt file
f=fopen('vhdl_out.txt','r');
A = fscanf(f,'%g',[1 inf]);
fclose(f);
subplot(224)
plot(B)
title('signal with white gaussian noise','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in decimal');%Digital to Analog conversion
for i=1:192
for j=1:256
if(A(i)== j )
outpt(i)=Vdelta(j);
end
end
end
subplot(223);
plot(outpt);
title('filtered sine wave sinewave output','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in decimal');
답변 (1개)
Thorsten
2016년 9월 5일
What's the size of the A you read? It's probably less than 192.
You can check using
A = fscanf(f,'%g',[1 inf]);
size(A)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!