Friday, April 8, 2011

#C_CODE: Extract bits

#include <stdio.h>
 
int main()
{
 int num,bits,pos,res;

 printf("Enter the num: ");
 scanf("%d",&num);
 printf("\nEnter the no. of bits to be extracted: ");
 scanf("%d",&bits);
 printf("\nEnter the pos(0 onwards): ");
 scanf("%d",&pos);
    
 res = (num >> (pos + 1 - bits)) & (~(~0 << bits));
 printf("\nThe extracted bits are: %d",res  );
 fflush(stdin);
 getchar();
}


EXAMPLE: num = 233(0b11101001), pos = 5, bits = 3, Result = 5(0b101)


Ref: C Programming Language - K&R

#C_CODE: squeeze(s,x) - Removes all occurances of character x from string s

/* Include files */
#include <stdio.h>
#include <string.h>
 

void squeeze(char *s,char x)
{
 int i,j;

 for(i=j=0; s[i] != '\0'; i++)
  if(s[i] != x)
   s[j++] = s[i];
 s[j] = '\0';
}

int main()
{
 char buff[35];

 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) - 1) = '\0';

 squeeze(buff,'a');
 printf("The output: %s\n",buff);
 getchar();
}


Ref: C Programming Language - K&R

Thursday, April 7, 2011

Leap Year

A year is a leap year, if it is divisible by 4 but not by 100, except that years divisible by 400 are leap years.

if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400 == 0) )
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);


REF: C Programming Language
Brian W. Kernighan &
Dennis M. Ritchie

Thursday, March 31, 2011

Unique device serial number

A unique device serial number for security and other purposes.
On power-up I would read the area of non-volatile memory that was intended to hold the serial number and other information.
If the information passed a validity check I would write the serial number into another special area of non-volatile memory.
If the validity check failed I would instead write a fictitious serial number.

Note: Our serial numbers were to be based on the production shift,production line, and the data and time of production.
I worked with the manufacturing group to create a serial number based on a non-existent production shift and line to guarantee my fictitious serial number would never be mistaken as genuine.

Mike Ficco - Cut & Paste Engineering

Friday, March 25, 2011

Sine wave generation in matlab/octave


frequency = 1000;
timeperiod = 1/frequency;
amplitude = 1;
dcoffset = 0;
t=0:0.00001:2*timeperiod;
out=dcoffset+amplitude*cos(2*pi*frequency*t);
plot(t,out)


Tested on Octave 3.2.4: March - 25, 2011



%Requirement: For plotting 2 periods of the signal
fs = 100000; %100KHz sampling frequency
f = 1000; %1KHz signal frequency
t = 0:1/fs:2*(1/f);
x = sin(2*pi*f*t);
plot(t,x)


%Requirement: For getting 10 sec samples

fs = 100000; %100Khz sampling frequency
f = 2; % 2Hz signal frquency
t = 0:1/fs:10-1/fs; %10sec sampleshttp://www.blogger.com/img/blank.gif
x = sin(2*pi*f*t);
plot(t,x)


%Requirement: For getting 100 samples
fs = 500; %500Hz sampling frequency
f = 100;
t = (0:100-1)/fs;
x = sin(2*pi*f*t);
plot(t,x)

Matlab Tutorial link

Monday, March 21, 2011

dBm and watts


  • dBm = (10Log10(milliWatts))


  • 1 mW = 0 dBm





  • Watts = 10((dBm - 30)/10)


  • milliWatts = 10(dBm/10)


  • Example:
    Gps Received Signal Power in watts = 1.41e-16W
    Gps Received Signal Power in milliwatts = 1.41e-13mW
    Gps Received Signal Power in dBm = -128.5dBm

    watts-dbm Calculator

    #C_CODE: Single Linked List Manipulations in C

    /* Details: Single linked list manipulation functions
    * Author : Vinod Sasidharan
    * Date : Monday, March 21, 2011, 11.41 am
    * Remarks: reverse, findNthNodeFromLast,deletedata
    */


    /* Include files */
    #include <stdio.h>
    #include <malloc.h>
     
    /* typedef for meaningful name */
    typedef struct singlelink node;
    
    /* The single linked list structure */
    struct singlelink
    {
        int data;
        node *link;
    };
    
    /* Function: append()
     * Appends the input data to the end of the linked list.
     * If it's the 1st node, then process it separately.
     */
    void append(node **q, int num)
    {
        node *temp = *q,*r;
        r = (node *)malloc(sizeof(node));
        r->data = num;
        r->link = NULL; //This would be the last node in list
    
        if(NULL == temp) // If it's going to be the 1st node...
            *q = r;
        else
        {
            //Traverse through the list to reach the last node
             while(NULL != temp->link) 
                temp = temp->link; 
    
            temp->link = r;
        }
    }
    
    /* Function: display()
     * Displays the linked list data on the standard output  
     */
    void display(node *temp)
    {
        printf("\nThe list: ");
        while(temp)
        {
            printf("%d ",temp->data);
            temp = temp->link;
        }
        printf("\n");
        getchar(); // Wait for user.
    }
    
    /* Function delatbeg()
     * Deletes the node at the beginning of the list
     */
    void delatbeg(node **q)
    {
        node *temp = *q;
        *q = temp->link;
        free(temp);
    }
    
    /* Function: addatbeg()
     * Adds a node at the beginning of the list with input data.
     */
    void addatbeg(node **q,int num)
    {
        node *r;
        r = (node *)malloc(sizeof(node));
        r->data = num;
        r->link = *q;
        *q = r;
    }
    
    /* Function: findNthNodeFrmLast()
     * Find the data in the Nth node(input) from last pos in
     * the list.
     */
    void findNthNodeFrmLast(node *temp,int pos)
    {
        node *Nth = temp;
        int i = 0;
    
        while(temp)
        {
            if(i++ >= pos) //The core logic
                Nth = Nth->link;
            temp = temp->link;
        }
        printf("Data in position %d node from last: %d\n",
                pos,Nth->data);
        getchar(); //Wait for user...
    }
    
    /* Function: reverse()
     * Reverse the linked list.
     */
    void reverse(node **q)
    {
        node *temp1 = *q;
        node *temp2 = NULL;
        node *temp3 = NULL;
    
        if(!temp1) return; //Sanity check
    
        while(temp1)
        {
            temp2 = temp1->link; // Save the next pointer
            temp1->link = temp3; // Save the previous
            temp3 = temp1; // Update the previous
            temp1 = temp2; // Update the next
        }
        *q = temp3; // Update the head node pointer.
    }
    
    /* Function: deletedata()
     * Deletes a particular data(input) from the list
     */
    void deletedata(node **q,int data)
    {
        node *temp = *q, *old = NULL;
        while(temp)
        {
            if(data == temp->data)
            {
                if(*q == temp)
                    *q = temp->link;
                else
                    old->link = temp->link;
    
                free(temp); // Free the node using the free().
                return;     // Expected exit.
            }
            else
            {
                old = temp;
                temp = temp->link;
            }
        }
    }
    
    /* Function: Main() 
     * Calls the single linked list manipulation functions 
     */
    int main()
    {
        /* Initialize the head node */
        node *head = NULL; 
        int i;
    
        /* Append 10 nos of data to the linked list */
        for(i=0; i<10; i++)
            append(&head,i);
    
        /* Display the linked list */ 
        display(head);
    
        /* Reverse the linked list & display it. */
        reverse(&head);
        printf("The reversed list: ");
        display(head);
    
        /* Find Nth node from last */
        findNthNodeFrmLast(head,3);
    
        /* delete a particular data */
        deletedata(&head,4);
        printf("Delete data 4: ");
        display(head);
    
        /*Delete the node at the beginning of the list */
        delatbeg(&head);
        delatbeg(&head);
        printf("Delete 1st two data: ");
        display(head);
    
        /* Add a data at the beginning of the list */
        addatbeg(&head,22);
        addatbeg(&head,25);
        printf("Add 22 & 25 at beginning: ");
        display(head);
    }

    Tested on: Visual C++ 2008 Express Edition, Windows XP