C Tutorial Part 2[1]

download C Tutorial Part 2[1]

of 19

Transcript of C Tutorial Part 2[1]

  • 8/8/2019 C Tutorial Part 2[1]

    1/19

    C Tutorial: Part 2

    Dr. Charalampos Tsimenidis

    Newcastle University

    School of Electrical, Electronic and Computer Engineering

    September 2010

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    2/19

    Loops: for

    Example: ex_p2_1.c

    1 int n, N=100;

    2 float fc=1000.0; //Carrier frequency in Hz

    3 float fs=48000.0; //Sampling frequency

    4 float t, Ts, wc, xc;

    5

    6 Ts=1.0/fs; //Sampling time

    7

    wc=2.0*M_PI*fc;8

    9 //Generate cosine signal

    10 for (n=0;n data.txt

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://goforward/http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    3/19

    Loops: while

    Example: ex_p2_2.c

    1 int n=0, N=100;2 float fc=1000.0; //Carrier frequency in Hz

    3 float fs=48000.0; //Sampling frequency

    4 float t, Ts, wc, xc;

    5

    6 Ts=1.0/fs; //Sampling time

    7 wc=2.0*M_PI*fc;8

    9 //Generate cosine signal

    10 while (n

  • 8/8/2019 C Tutorial Part 2[1]

    4/19

    Loops: do

    Example: ex_p2_2.c

    1 int n=0, N=100;2 float fc=1000.0; //Carrier frequency in Hz

    3 float fs=48000.0; //Sampling frequency

    4 float t, Ts, wc, xc;

    5

    6 Ts=1.0/fs; //Sampling time

    7 wc=2.0*M_PI*fc;8

    9 //Generate cosine signal

    10 do

    11 {

    12 t=n*Ts;

    13 xc=cosf(wc*t);

    14 printf("%+.7f, %+.7f\n",t,xc);

    15 }

    16 while(++n

  • 8/8/2019 C Tutorial Part 2[1]

    5/19

    Structures: struct

    Definition 1:

    1 struct cmplx

    2 {

    3 float real;

    4 float imag;

    5 };

    Example: ex_p2_4.c

    1 struct cmplx x;

    2

    3

    x.real=1.0;4 x.imag=1.0;

    5 printf("Real-Imaginary form: x=%f+j*%f\n",x.real,x.imag);

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    6/19

    Structures: struct, typedef

    Definition 2:

    1 typedef struct

    2 {

    3 float real;

    4 float imag;

    5 }cmplx;

    Example: ex_p2_5.c

    1 cmplx x;

    2

    3

    x.real=1.0;4 x.imag=1.0;

    5 printf("Real-Imaginary form: x=%f+j*%f\n",x.real,x.imag);

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    7/19

    Arrays (1)

    Definitions: ex_p2_6.c

    1 float x[1000];

    2 float y[]={1.0,1.0, 1.0};

    3 float z[1000]={1.0};

    4 int adc[1024];5 char text[]="C is cool!";

    Array index limits for an array with N elements:

    Lower index limit: 0

    Upper index limit: N-1

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    8/19

    Arrays (2)

    Example: ex_p2_7.c

    1 int n, N=100;

    2 float fc=1000.0; //Carrier frequency in Hz

    3 float fs=48000.0; //Sampling frequency

    4 float t, Ts, wc, xc[N];

    5

    6 Ts=1.0/fs; //Sampling time7 wc=2.0*M_PI*fc;

    8

    9 //Generate cosine signal

    10 for (n=0;n

  • 8/8/2019 C Tutorial Part 2[1]

    9/19

    Two Dimensional Arrays

    Example: ex_p2_8.c1 float X[3][3];

    2 float Y[3][3]={{1.0, 0.0, 0.0},

    3 {0.0, 1.0, 0.0},

    4 {0.0, 0.0, 1.0}};

    5 int m, n;

    6

    7 for (m=0;m

  • 8/8/2019 C Tutorial Part 2[1]

    10/19

    Functions (1)

    Definition: ex_p2_9.c

    1 typedef struct

    2 {

    3 float real;

    4 float imag;

    5 float mag;

    6 }cmplx;7

    8 float mag(cmplx x)

    9 {

    10 float y;

    11 y=sqrtf(x.real*x.real+x.imag*x.imag);

    12

    13 return y;

    14 }

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    F i (2)

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    11/19

    Functions (2)

    Example: ex_p2_9.c

    1 cmplx x;

    2

    3 x.real=1.0;

    4 x.imag=1.0;

    5

    6 x.mag=mag(x);

    7 printf("The magnitude of x = %+.3f %+.3f * j is %f\n",x.real

    ,x.imag,x.mag);

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    F ti (3)

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    12/19

    Functions (3)

    Functions with array input variables: ex_p2_10.c

    1 float mean(float x[], unsigned long N)

    2 {

    3 int n;

    4 float avg=0;

    5

    6 for (n=0;n

  • 8/8/2019 C Tutorial Part 2[1]

    13/19

    Functions (4)

    Functions using global variables: ex_p2_11.c

    1 // x[] and N are globally defined (above the main function)

    2 float mean()

    3 {

    4

    5 int n;6 float avg=0;

    7

    8 for (n=0;n

  • 8/8/2019 C Tutorial Part 2[1]

    14/19

    Pointers

    Pointer is a variable that points to or references a memorylocation where data are stored

    Pointer declarations examples: ex_p2_12.c

    1 float *p1, *p2;

    2 int *p3;

    Address operator: &1 float *p1, *p2;

    2 float num1=10.0, num2=0.0;

    3

    4 p1=&num1; //p1 points to the address of num1

    5 p2=p1; //p2 point to the same address as p16 num2=*p1; //Equivalent to num2=num1;

    7

    8 printf("num2 = %f\n",num2);

    9 printf("*p2 = %f\n",*p2);

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    Pointer and Arrays

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    15/19

    Pointer and Arrays

    Example: ex_p2_13.c

    1 float *p1=NULL, x[5]={0.1, 0.2, 0.3, 0.4, 0.5}, y1, y2;

    2 p1=&x[0]; // point p1 to the first element of x[]

    3

    y1=*(p1+3); // Equivalent to y1=x[3];4 y2=p1[3]; // Equivalent to y2=x[3];

    5

    6 printf("y1 = %f\n",y1);

    7 printf("y2 = %f\n",y2);

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    Bitwise Operators (1)

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    16/19

    Bitwise Operators (1)

    Memory Range Data Type

    Logical Operators bitwise complement ~bitwise AND &

    bitwise OR |bitwise XOR ^

    Shift Operators left shift >

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    Bitwise Operators (2)

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    17/19

    Bitwise Operators (2)

    Example 1: ex_p2_14.c1 unsigned int a=0, b=3;

    2

    3 //Left and right shift

    4 printf("a=%u\n",a); // a=0

    5 a=b=1; // or a=a>>1

    8 printf("a=%u\n",a); // a=6

    Example 2: ex_p2_15.c1 unsigned short int a=1, b=5;

    2

    3 //Bitwise operations4 printf("~a = %hu\n",~a); //Complement:

    5 printf("a & b = %u\n",a&b); //AND

    6 printf("a | b = %u\n",a|b); //OR

    7 printf("a ^ b = %u\n",a^b); //XOR

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    Review Exercise 3

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    18/19

    Review Exercise 3

    Write a program that generates in an array and prints a linearfrequency modulated chirp with the following parameters:

    Start frequency: f0=1 kHzStop frequency: f1=2 kHzSampling frequency: fs=48 kHz

    Duration: Td=50 msThe chirp formula is given as:

    y(t) = cos

    2

    f0t+

    f1 f02Td

    t2

    Plot the generated signal in MatlabUse both for, while, as well as the do approaches

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    Review Exercise 4

    http://find/http://goback/
  • 8/8/2019 C Tutorial Part 2[1]

    19/19

    Review Exercise 4

    Extend the structure for the complex numbers to include thephase argument of a complex number

    Write a function, cphi, that computes phase argument ofcomplex number

    Write 4 functions that implement addition, subtraction,

    multiplication and division for complex numbers with namescadd, csub, cmul, and cdiv. Test the developed functions forz1=-1+j and z2=2-j. Check the results using matlab.

    Create your own complex-number library by including all the

    developed functions and cmplx structure itself in a header filecmplx.h. Re-write the previous program to use#include "cmplx.h"

    Dr. Charalampos Tsimenidis C Tutorial: Part 2

    http://find/http://goback/