/***************************************************************/ /*Copyright (c) by Continocean Tech Inc. */ /*12 Mountain Ave., Montville, New Jersey, USA 07045 */ /*Tel: 201-257-1912 Fax: 201-257-9634 */ /*Email: continocean@webexpert.net */ /*All rights reserved. No part of this work covered by the */ /*copyright hereon may be reproduced or used in any form or by */ /*any means----graphic, electronic, or mechanical, including */ /*photocopying, recording, taping, or information storage and */ /*retrieval systems----without written permission of */ /*Continocean Tech inc. */ /***************************************************************/ ENGSIM for MS VC++ Engineering Simulation Library for Microsoft Visual C++ Usage of ENGSIM Instructions IMPORTANT MESSAGE: Please use the Microsoft Development Studio's File-OpenWorkspace to open \engsim\sim project space. Then use File-Open to open file main.cpp for editing. There are several examples under \engsim\examples which are included in main.cpp. You can edit main.cpp and the examples for exercises. PART I. COMPLEX, MATRIX, COMPLEX MATRIX, INTEGER MATRIX AND STRING MATRIX Complex declare a complex number e.g. Complex a; matrix declare a matrix matrix a(2,2), h(10,20), p(200); //a(1,1)...... a(2,2) //p(1) ...... p(200) matrixn declare a matrix with negative indexing matrixn a(2,2); //a(-2,-2) ...... a(2,2) matrixn p(100); //p(-100) ......p(100) cmatrix declare a complex matrix cmatrix a(2,2),b(2,2),p(100); cmatrixn declare a complex matrix with negative indexing cmatrixn a(2,2),b(2,2),p(100); //a(-2,-2)......a(2,2), p(-100)......p(100) imatrix declare an integer matrix imatrix a(2,2), b(2,2), p(100); //a(1,1) ...... a(2,2) //p(1) ...... p(100) imatrixn declare an integer matrix with negative indexing imatrixn a(2,2),b(2,2),p(100); //a(-2,-2) ... a(2,2) //p(-100) ... p(100) smatrix declare a string matrix smatrix a(2,2),p(100); //a(1,1) ... a(2,2) //p(1) ... p(100) = equal operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn b= 1.0+2.0*J; b=a; + plus operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn c= a+b; - minus operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn c= a-b; c=-b; * multiplication operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn c= a*b; / division operator for complex, matrix, matrixn, cmatrix, cmatrixn c=a/b; ~ conjugate operator for complex, cmatrix, cmatrixn c= (~a); == logic equal operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix if(a==b){...}; != logic not equal operator for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix while(a!=b){...}; | or for imatrix, imatrixn c=a|b; & and for imatrix, imatrixn c=a&b; ^ X-or for imatrix, imatrixn c=a^b; ( , ) an element of a matrix, matrixn, cmatrix, cmatrixn imatrix, imatrixn, smatrix a(2,-5)=10.1+2.1*J; p(-5)=3.0+9.9*J; J pure unit imaginary for Complex a= 1.0+2.0*J; abs() absolute for complex take absolute of each element for matrix, matrixn, cmatrix, cmatrixn Complex ca; double dc; dc=abs(ca); cmatrix a(3,3); matrix b(3,3); b=abs(a); amplitude() amplitude of cmatrix and cmatrixn cmatrix a(10); matrix z(10); z=amplitude(a); cmatrixn a(10); matrixn z(10); z=amplitude(a); arg() same as phase() corrm(a, b, n) n-by-n correlation matrix of a and b for cmatrix, cmatrixn c=corrm(a,b,2); det() determinant of a matrix, matrixn, cmatrix, cmatrixn imatrix, imatrixn double dc; dc=det(a); eigen(a,d,z) compute eigenvalues d(1) ... d(n) and eigen vectors z(1,1) z(1,2) (......)=z1 (......)=z2 ...... z(n,1) z(n,2) a*z1=d(1)*z1 a*z2=d(2)*z2 ...... matrix a(2,2); matrix d(2),z(2,2); //if a is symmetric eigen(a,d,z); cmatrix d(2),z(2,2); //if a is nonsymmetric eigen(a,d,z); exp() exponential for complex exp(J*2.0*PI); float2string(fa, sa, len) convert float number fa to string sa(1) ... sa(len) fprintf(,) print to a disk file for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix, double, integer, character FILE *fp; fp=fopen("file.out","w"); fprintf(fp, c); fclose(fp); fprintf("file.out",c); //append only fscanf( , ) read from a disk file for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix, double, integer, character FILE *fp; fp=fopen("file.in","r"); fscanf(fp, a); fclose(fp); HH hermitian for cmatrix, cmatrixn c=HH(a); identity identity matrix (diagonal elements are all 1's, others all 0's) (for matrix only) a=identity(10); //10x10 identity matrix identityn identity matrix with negative indexing (for matrix with negative indexing only) a=identityn(10); //21x21 identity matrixn init(, ...) initialize a matrix, matrixn, cmatrix, cmatrixn imatrix, imatrixn, smatrix matrix a(2,2); init(a, 1.0,2.0, 4.0,5.0 ); matrixn a(2,2); a=init(a,1.0,2.0,3.0,4.0,5.0, 2.0,2.0,3.0,4.0,5.0, 3.0,2.0,3.0,4.0,5.0, 4.0,2.0,3.0,4.0,5.0, 5.0,2.0,3.0,4.0,5.0 ); cmatrix a(2,2); a=init(a, 1.0+2.0*J, 2.0+3.0*J, 1.1+2.1*J, 2.1+3.1*J ); cmatrixn a(2,2); a=init(a, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 1.1+2.1*J, 2.1+3.1*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 1.1+2.1*J, 2.1+3.1*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J, 2.0+3.0*J, 1.0+2.0*J ); imatrix a(2,2); init(a, 1,2, 4,5 ); imatrixn a(2,2); init(a, 1,2,3,2,3, 4,5,6,2,3, 7,8,9,2,3, 4,5,6,2,3, 7,8,9,2,3 ); smatrix a(2,2); a=init(a, 'H','i', 'O','K'); imag() imaginary of a complex number double da; da=imag(a); imaginary of Complex matrix matrix mc(2,2); mc=imag(a); matrixn mc(2,2); mc=imag(a); int2string(ia, sa, len) convert integer ia to string sa(1) ... sa(len) joinr(a, b); right join matrices a,b as (a b) for matrix, cmatrix, imatrix joind(a, b); down join matrices a,b as a(1,1)...... ............ ( b(1,1)...... ) ............ for matrix, cmatrix, imatrix log2(x) base 2 log, x double log(x,n) base n log, n=integer, x double log(x) base e log, x Complex Max() find maximum of all elements for matrix double a_max=Max(a); Min() find minimum of all elements for matrix double a_min=Min(a); Max( , , ,) find maximum of all elements for matrix Max(a, y_max, i_max, j_max); //y_max=a(i_max,j_max) Min( , , ,) find minimum of all elements for matrix Min(a, y_min, i_min, j_min); //y_min=a(i_min,j_min) mean(a) mean of matrix, cmatrix double x; matrix a; x=mean(a); Complex y; cmatrix b; y=mean(b); normsq() real*real+imag*imag for complex sum a(i,j)*a(i,j) for matrix, matrixn, imatrix, imatrixn sum a(i,j)*(~a(i,j)) for cmatrix, cmatrin double dc; dc=norm(a); phase() phase of a complex number, cmatrix and cmatrixn Complex c; double x; x=phase(c); cmatrix a(10); matrix z(10); z=phase(a); cmatrixn a(10); matrixn z(10); z=phase(a); pow( , ) power for complex number Complex c; pow(c,2.0); pow(c,3); pow(c,1.0+J*2.0); powi(x,n) x^n n=integer printw() print to screen for complex, matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix, double, integer, character printw(a); printw(2.0+3.0*J); printw(1.0); printw(1); printw('c'); printw("test=%g, dog=%d\n",0.5, 100); real() real of a complex number double da; da=real(a); real of Complex matrix matrix mc(2,2); mc=real(a); matrixn mc(2,2); mc=real(a); shiftl( ) shift left by 1 for matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn shiftr( ) shift right by 1 for matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn shiftu() shift up by 1 for matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn shiftd() shift down by 1 for matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn sub(a, m1, n1, m2, n2) Take submatrix beginning at a(m1,n1) and ending at a(m2,n2) for matrix, matrixn, cmatrix, imatrix (matrixn becomes a matrix) sub(a,m1,n1,m2,n2); sum(a) sum for matrix, cmatrix double x; matrix a; x=sum(a); Complex y; cmatrix b; y=sum(b); TT() transpose for matrix, matrixn, cmatrix, cmatrixn, imatrix, imatrixn, smatrix c=TT(a); unitary unitary matrix (all elements are 1's) (for matrix) a=unitary(10); //10x10 unitary matrix a=unitary(5,10); //5x10 unitary matrix unitaryn unitary matrix with negative indexing (for matrix with negative indexing) a=unitaryn(10); //21x21 unitary matrixn var(a) variance for matrix, cmatrix double x; matrix a; x=var(a); cmatrix b; x=var(b); PART II. GRAPHICS (2D and 3D) In 2D plotting, if y(k)>=1.0e40, the point k is ignored. The feature is useful for plotting y1,y2,... with different x(k). Simply combining x1,x2,... into x and let unwanted y1(k),y1(k),...=1.0e40. The dimension of x and y may need to be adjusted. In 3D plotting, if y(k)>=1.0e40, the point is interrupted and the curve is restarted from the next point. The feature is useful for plotting 3D objects to avoid unwanted lines. Simply insert z(k)=1.0e40 to delete a line between z(k-1) and z(k+1). plot(y) plot array y matrix y(100); plot(y); plot(x, y) plot array, x=X-axis, y=Y-axis matrix x(100), y(110); plot(x, y); plot(x, y1, y2) plot array, x=X-axis, y1,y2,...=Y-axis matrix x(100), y1(110), y2(100); plot(x, y1, y2); plot(x, y1, y2, y3) plot(x, y1, y2, y3, y4); plot(x, y1, y2, y3, y4, y5); plot(y, "legend"); plot(x, y, "legend"); plot(x, y1, "legend1", y2, "legend2"); plot(x, y1, "legend1", y2, "legend2", y3, "legend3"); plot(x, y1, "legend1", y2, "legend2", y3, "legend3", y4, "legend4"); plot(x, y1, "legend1", y2, "legend2", y3, "legend3", y4, "legend4", y5, "legend5"); plot3(x, y, z) plot 3-D curve using data array, x=X-axis, y=Y-axis, z=Z-axis matrix x(100), y(110), z(110); plot3(x, y, z); plot3(x1, y1, z1, x2, y2, z2) plot 3-D curve using data array, x=X-axis, y=Y-axis, z=Z-axis matrix x1(100), y1(110), z1(110), x2(100), y2(100), z2(110); plot3(x1, y1, z1, x2, y2, z2); plot3(x, y, z, "legend"); plot3(x1, y1, z1, "legend1", x2, y2, z2, "legend2"); In the above plot(), all x,y,z are matrix (array). In the following plot(), x is cmatrix. plot(cmatrix x); plot(cmatrix x, "legend"); plot(cmatrix x1, cmatrix x2); plot(cmatrix x1, "legend1", cmatrix x2, "legend2"); printt(x, y, " ", number1, ...) print any text or number to a figure with position (x,y) x,y are type-int. -100000, counter-clockwise rotation. set_rotation_angle(-PI/24,PI/12,-PI/6); //default set_rotation_angle(PI/24,PI/12,-PI/6); set_focal_distance(x) for 3D. default is 100 times the difference between the maximum and minimum X-coordinates. set_focal_distance(101.0); set_picture_size(x); scale picture x=1.0 default size 0.00 set_text_position(100, 20); set_text_size(x); set text font size set_text_size(10); //default set_text_size(15); PART III. COMMUNICATIONS ENGINEERING AND SIGNAL PROCESSING SIMULATION betai(a, b, x) Incomplete Beta function I_x(a,b) 00 double x=0.1,a1.0,b=0.1,y; y=betai(a, b, x); convol(x, y) convolution of arrays x and y for matrix, matrixn, cmatrix and cmatrixn matrix x(100),y(100),z(200); z=convol(x,y); matrixn xn(100),yn(100),zn(200); zn=convol(xn,yn); cmatrix cx(100),cy(100),cz(200); cz=convol(cx,cy); cmatrixn cxn(100),cyn(100),czn(200); czn=convol(cxn,cyn); corr(x, y) correlation of arrays x and y for matrix (array) and cmatrix (array) matrix x(100), y(100); matrixn z(100); //z(-100) ... z(100) z=corr(x,y); cmatrix x(100), y(100); cmatrixn z(100); //z(-100) ... z(100) z=corr(x,y); corrnorm(x, y) normalized correlation of arrays x and y for matrix (array) and cmatrix (array) matrix x(100), y(100); matrixn z(100); //z(-100) ... z(100) z=corrnorm(x,y); cmatrix x(100), y(100); cmatrixn z(100); //z(-100) ... z(100) z=corrnorm(x,y); crandom(); generate uniform-distributed integer in (0, 2147483647) int a; a=crandom(); czt(x, m, w, a) Chirp-Z Transform Complex a=5.0*exp(J*PI/4); //starting point Complex w=0.999*exp(J*PI/6/50); //spiral rate int m=200; matrix x(100); cmatrix X(m); X=czt(x,m,w,a); fft(matrix &h) compute spectrum of an array h using FFT sampling-rate=1 n=number of taps of h if n0) is the kth time. (setting k=1 and k=1000 respectively gives two-ray channel or diversity channel) cmatrixn c1(100), c2(100); float fm=75.0, T=41.0e-6; c1(0)=ray_fade(fm, T, 1); c2(0)=ray_fade(fm, T, 1000); //c1 and c2 are two independent rays. rcos_sr(float b, float T, float step, int k): Square-root raised-cosine pulse-shaping function. b=rolloff parameter spectrum is T 0 <= |f| <= (1-b)/T/2 X(f)= T cos{(PI*T/b/2)*[|f|-(1-b)/T/2]} (1-b)/T/2 <= |f| <= (1+b)/T/2 0 otherwise step*k=time point matrixn h(16); float b=0.3, T=40.0e-6, step=T/4; //each symbol has 4 samples, 4 times oversampling for(int k=-16;k<=16;k++) h(k)=rcos_sr(b, T, step, k); Ref: J. Proakis, Digital Communications, 2ed edition, McGraw-Hill, 1989. rcos_ps(matrixn &h, int n, float b, float T, int m): rcos_ps(matrix &h, int n, float b, float T, int m): compute filter taps of square-root raised-cosine pulse-shaping matrixn h: h(-n),..., h(0),...h(n)=filter taps, 2n+1=number of taps matrix h: h(1),...h(n)=filter taps, n=number of taps b=rolloff parameter T=symbol period T/m=sampling-period, default m=4 matrixn h(16); float b=0.3, T=40.0e-6; //each symbol has 4 samples, 4 times oversampling rcos_ps(h, 16, b, T): //default m=4, matrixn h has 2*16+1 taps int m=2; matrix g(17); rcos_ps(g, 17, b, T, m): //m=2, matrix g has 2*8+1=17 taps Ref: J. Proakis, Digital Communications, 2ed edition, McGraw-Hill, 1989. select(k, a) Select the kth smallest number in an array a(1),...,a(n). Returned array is reordered in such a way that the kth element is the kth smallest with all the smaller elements moved to the left-hand side of it and all the larger elements moved to the right-hand side of it. int n=100; matrix a(n),x(n); x=select(10,a); sign(x) return the sign (1.0 or -1.0) of x double x,xs; xs=sign(x); sinc(x); sinc function sin(x)/x double y; y=sinc(1.5*PI); sa(x); same as sinc function sin(x)/x spectrum(x, sp_freq, sp_amp, sp_phase, n, Ts); compute spectrum of an array x using FFT n=pow(2,r)=number of points in FFT, dimension of sp_freq (frequency), sp_amp (amplitude) and sp_phase (phase) is n/2 Ts=sampling-period int n=4098; //2^12 matrix x(n), sp_freq(n/2), sp_amp(n/2), sp_phase(n/2); float Ts=0.01; fft(x, sp_freq, sp_amp, sp_phase, n, Ts); plot(freq, sp_amp, "Amplitude", sp_phase, "Phase"); spectrum(func, sp_freq, sp_amp, sp_phase, n, T); compute spectrum of function func T=function period, from 0 to T n=pow(2,r)=number of points in FFT, dimension of sp_freq (frequency), sp_amp (amplitude) and sp_phase (phase) is n/2 sampling-period=T/n float function(float x) {...... } int n=4098; //2^12 matrix sp_freq(n/2), sp_amp(n/2), sp_phase(n/2); float T=100.0; spectrum(function, sp_freq, sp_amp, sp_phase, n, T); plot(freq, sp_amp, "Amplitude", sp_phase, "Phase"); tail(func, a) integrate the tail of the function func from a to +infinity double function(double x){ double y; ...... return y; } double a=1.0; tail(func,a); t_stat(a1, a2) Compute Student-t statistic for testing the difference of means for two arrays a1(1)...a1(n1), a2(1)...a2(n2) with the SAME variance matrix a1(100),a2(100); double y; y=t_stat(a1, a2); t_test(data1, data2); Compute Student-t test for testing the difference of mean for two arrays a1(1)...a1(n1), a2(1)...a2(n2) with the SAME variance. A very small returned value (<0.05) implies that the means are significantly different matrix a1(100),a2(100); double y; y=t_test(a1, a2); t_statv(a1, a2) Compute Student-t statistic for testing the difference of means for two arrays a1(1)...a1(n1), a2(1)...a2(n2) with DIFFERENT variance matrix a1(100),a2(100); double y; y=t_statv(a1, a2); t_testv(data1, data2); Compute Student-t test for testing the difference of mean for two arrays a1(1)...a1(n1), a2(1)...a2(n2) with DIFFERENT variances. A very small returned value (<0.05) implies that the means are significantly different matrix a1(100),a2(100); double y; y=t_testv(a1, a2); ztransform(matrix &h,matrix &f,matrix &a,matrix &p,int m,int n,float T): compute spectrum of an FIR filter using Z-transform h(m)=taps of an FIR filter=h(1)+h(2)z^{-1}+...+h(m)z^{-(m-1)} f(n)=array of frequencies a(n)=array of amplitudes of H(z)=H(e^(2*PI*f*T)) p(n)=array of phases of H(z)=H(e^(2*PI*f*T)) m=number of taps of h n=number of discrete frequencies T=sampling period of h, 1/T=freq-domain-period int n=4096,m=33; matrix h(n), sp_amp(n/2), sp_freq(n/2), sp_phase(n/2); float T=float(41.0e-6), b=(float)0.3; rcos_ps(h, 33, b, T); //square-root raised-cosine pulse-shaping ztransform(h, sp_freq, sp_amp, sp_phase, m, n, Ts); set_axis_title("Frequency (Hz)"," "); set_caption("Spectrum of an FIR filter using Z transform"); plot(sp_freq, sp_amp, "Amp");