How to resolve the error of "Consider preallocating for speed" which is coming on executing the program below??

2 views (last 30 days)
f=input('enter frequency of fundamental');
K=input('enter number of SAMPLES PER CYCLE');
C=input('enter number of cycles required ');
T=1/f;
Ts=T/K;
k=0:1:K*C;
t=(k)*Ts;
v=10*sin(2*pi*f*t+pi/6);
g=2*pi*f*t;
subplot(2,2,1);
plot(t,v)
title('signal without noise');
xlabel('sample nummber');
ylabel(' magnitude');
%calculation of the Vpeak amd phase with two sample window technique WITHOUT noise
for k=1:1:K*C
a(1,k+1)=(v(1,k)*cos(g(1,k+1))-v(1,k+1)*cos(g(1,k)))/sin(g(1,k+1)-g(1,k));
b(1,k+1)=(-v(1,k)*sin(g(1,k+1))+v(1,k+1)*sin(g(1,k)))/sin(g(1,k+1)-g(1,k));
V(1,k+1)=sqrt(a(1,k+1)^2+b(1,k+1)^2);
G(1,k+1)=atan(b(1,k+1)/a(1,k+1));
end
k=1:1:K*C;
V1(1,k)=V(1,k+1);
mean(V1)
std(V1);
l=1;
%calculation of the Vpeak amd phase with two sample window technique WITH noise
for E=0.1:0.3:3.0
s=rng;
a=0;b=05;
r = (b-a).*rand(1,length(t)) + a;
rng(s);
vn=10*sin(2*pi*f*t+pi/6)+E*r;
subplot(2,2,2);
j=1:1:length(vn);
plot(j,vn)
title('signal with noise');
xlabel('sample nummber');
ylabel(' magnitude');
for k=1:1:K*C
a(1,k+1)=(vn(1,k)*cos(g(1,k+1))-vn(1,k+1)*cos(g(1,k)))/sin(g(1,k+1)-g(1,k));
b(1,k+1)=(-vn(1,k)*sin(g(1,k+1))+vn(1,k+1)*sin(g(1,k)))/sin(g(1,k+1)-g(1,k));
Vn(1,k)=sqrt(a(1,k+1)^2+b(1,k+1)^2);
Gn(1,k)=atan(b(1,k+1)/a(1,k+1));
end
j=1:1:C*K;subplot(2,2,3);
plot(j,Vn);
title('estimated peak with E=3.0');
xlabel('sample nummber');
ylabel('peak magnitude estimated');
M(1,l)=mean(Vn)
p(1,l)= std(Vn)
l=l+1;
end
%-----------------frequency response-----------------------
for m=1:1:1000
vf=(10)*sin(2*pi*m*t+pi/6);
for k=1:1:K*C
a(1,k+1)=(vf(1,k)*cos(g(1,k+1))-vf(1,k+1)*cos(g(1,k)))/sin(g(1,k+1)-g(1,k));
b(1,k+1)=(-vf(1,k)*sin(g(1,k+1))+vf(1,k+1)*sin(g(1,k)))/sin(g(1,k+1)-g(1,k));
Vf(1,k+1)=sqrt(a(1,k+1)^2+b(1,k+1)^2);
Gf(1,k+1)=atan(b(1,k+1)/a(1,k+1));
end
k=1:1:K*C;
Vf1(1,k)=Vf(1,k+1);
me(1,m)=mean(Vf1);
end
subplot(2,2,4)
m=1:1:1000;
plot(m/50,me);
title('frequency response ');
xlabel('m');
ylabel('magnitude');

Answers (2)

Stephan
Stephan on 18 Nov 2020
Edited: Stephan on 18 Nov 2020

Ameer Hamza
Ameer Hamza on 18 Nov 2020

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!