Showing posts with label Kathmandu Engineering College. Show all posts
Showing posts with label Kathmandu Engineering College. Show all posts

Random File Access in C

WAP to print the first character of each word from the string contained in a file “details.txt”. The file “details.txt” has “Deep Raj Bhujel” string in it and your program should print “D R B” on the console screen. [Random File Access: fseek(), ftell(), rewind()]

#include<stdio.h>

#include<stdlib.h>

int main()

{

    FILE *fp;

    char ch;

    //long pos;


    fp = fopen("details.txt","r");


    if(fp == NULL)

    {

        puts("Error opening file");

        exit(1);

    }


    //printf("%ld ",ftell(fp));

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,4,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,3,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    //rewind(fp);

    //printf("%ld ",ftell(fp));


    return 0;

}


Implementation of Preprocessor in C Programming

Q. Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle. Store these macro definitions in a file called "areaperi.h". Include this file in your program, and call the macro definitions for calculating area and perimeter for different triangles, squares and circles.

Step 1: Create a header file (.h) declaring the prototypes of required functions.

float area1(int, int, int); // for triangle
int area2(int); // for square
float area3(float); // for circle

float perimeter1(int, int, int); // for triangle
int perimeter2(int); // for square
float perimeter3(float); // for circle

Step 2: Create a C programming file (.c) defining those functions.

#include<stdio.h>
#include<math.h>
#define PI 3.1416

float perimeter1(int a, int b, int c) // for triangle
{
    return a+b+c;
}

int perimeter2(int a) // for square
{
    return 4*a;
}

float perimeter3(float r) // for circle
{
    return 2*PI*r;
}

float area1(int a, int b, int c) // for triangle
{
    float s = perimeter1(a, b, c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

int area2(int a) // for square
{
    return pow(a,2);
}

float area3(float r) // for circle
{
    return PI*r*r;
}

Step 3: Write your C program (.c) including the areaperi.h file as #include "areaperi.h".

Standard AM, FM and PM Equations

Standard Mathematical Equations for AM, FM and PM

1. Amplitude Modulation (AM)

  • : Carrier amplitude, : Carrier frequency
  • :  Message signal amplitude, : Message signal frequency
  • : Modulation index
  • : Carrier Power, : Sideband Power, : Total Power
  • : Efficiency
  • : Bandwidth of the AM signal

Amplitude Modulation

Q. The equation of amplitude modulated wave is given by s(t) = 40[1+0.8cos(2π ×10^3t)]cos(4π×10^5t). Find the carrier power, the total sideband power and bandwidth of the signal. The value of resistor given is 30Ω.
Solution: Here,

We are given:

s(t)=40[1+0.8cos(2π103t)]cos(4π105t)

And:

  • Load resistance R=30ΩR = 30\, \Omega

Step 1: Identify the standard AM form

Standard AM wave:

s(t)=Ac[1+μcos(2πfmt)]cos(2πfct)

From the given equation:

  • Ac=40A_c = 40

  • μ=0.8

  • fm=103=1kHz

  • fc=4π1052π=2×105=200kHz

a) Carrier Power PcP_c

Pc=Ac22R=402230=160060=26.67W​

Frequency Modulation

Q. A single tone FM is represented by the voltage equation as v(t)=12cos(5x10^8t+5sin1250t). Determine following:

a) Carrier frequency

b) Modulating frequency

c) Modulation index

d) Maximum frequency deviation

Solution: Here,

We are given a single-tone FM signal:

v(t)=12cos(5×108t+5sin(1250t))

This equation is in the general form of an FM signal:

v(t)=Accos(2πfct+βsin(2πfmt))

Where:

  • fcf_c = carrier frequency

  • fmf_m = modulating frequency

  • β\beta = modulation index

  • Δf=βfm\Delta f = \beta f_m = frequency deviation

Step-by-step Extraction from Given Equation

We compare:

v(t)=12cos(5×108t+5sin(1250t))

with:

v(t)=Accos(ωct+βsin(ωmt))

From this, we get:

  • ωc=5×108fc=ωc2π=5×1082π79.58×106=79.58MHz\omega_c = 5 \times 10^8 \Rightarrow f_c = \frac{\omega_c}{2\pi} = \frac{5 \times 10^8}{2\pi} \approx 79.58 \times 10^6 = 79.58 \, \text{MHz}

  • ωm=1250fm=12502π199Hz\omega_m = 1250 \Rightarrow f_m = \frac{1250}{2\pi} \approx 199 \, \text{Hz}

  • β=5\beta = 5

Amplitude Modulation

Q. An audio frequency signal 10sin(1000πt) is used for a single tone amplitude modulation with a carrier of 50sin(2π×10^(5)t). Calculate:
(i) Modulation index
(ii) Bandwidth requirement
(iii) Total power delivered if load = 60Ω.

Solution: Here,

We are given:

  • Message signal: m(t)=10sin(1000πt)

  • Carrier signal: c(t)=50sin(2π×105t)

  • Load Resistance: R=60ΩR = 60\, \Omega

Let’s solve the parts step-by-step.

(i) Modulation Index μ\mu

The modulation index is defined as:

μ=AmAc​​

Where:

  • Am=A_m =Amplitude of message = 10

  • Ac= Amplitude of carrier = 50

μ=1050=0.2

So, Modulation Index = 0.2

Analog vs Digital Communication System

Feature

Analog Communication

Digital Communication

Signal Type

Continuous-time (sine wave, etc.)

Discrete-time (binary 0s and 1s)

Noise Immunity

Low (prone to distortion)

High (noise can be detected/corrected)

Bandwidth Requirement

Usually, lower

Usually, higher

Transmission Quality

Degrades with distance

Maintains quality with regeneration

Security

Less secure

More secure (encryption possible)

Cost

Cheaper hardware

Costlier due to encoding/processing

Multiplexing

Frequency Division Multiplexing (FDM)

Time/Frequency/Code Division Multiplexing

Applications

Analog radio, landline telephony

Internet, mobile phones, satellite links

Signal Processing

Harder to analyze and process

Easier (digital circuits, software tools)




Line Spectrum of Signal

Q. Plot the line spectrums of x(t) =12 + 6sin (140πt +30°) - 9cos(80πt-70°).

Solution:

Let’s Analyze and Plot the Line Spectrum of

x(t) = 12 + 6sin(140πt+300) − 9cos(80πt−700)

Step 1: Convert all terms to cosine form

Use the identity: sin(θ) = cos(θ−900)

So,

6sin(140πt+300) = 6cos(140πt−600)

−9cos(80πt−700) = 9cos(80πt+1100)

Thus, x(t) = 12 + 6cos(140πt−600) + 9cos(80πt+1100)

Step 2: Frequencies, Amplitudes, Phases

Term

Frequency (Hz)

Amplitude

Phase

12 (DC)

0

12

6cos(140πt−600)

f=70

6

-60°

9cos(80πt+1100)

f=40

9

+110°

 We split each cosine into two spectral lines at ±f with half the amplitude.