f



c-code to matlab m code

hi am kaushik i would like to know if this code can be directly converted to matlab. if not what can is to be done to run it matlab. ma posting the code here. please reply as soon as possible

code here

/*program used for acousto ultrasonic signal processing*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
main()
{
int       i=        0,           x,j,          a,b,max1=0,sum1=0,lpeak=0,ccount=0,threshold;
int
cpeaktime,rabn,cfthtime,cduration,clthtime,crisetime,th1,th2,n1=0,n2=0;
float hindex;
float cenergy=0,cenergy1;
FILE *fp;char dataf1[8];
char wd[8];
int a1,b1,j1,y1,y2=0,i1,k1;
int graphdriver=DETECT,graphmode;
clrscr();
window(1,1,80,25);
textcolor(4);
textbackground(7);
gotoxy(1,1);
cprintf("        Acousto Ultrasonic Measurements          ");
textcolor(15);
textbackground(4);
gotoxy(1,24);
cprintf("  school of biomedical engineering,it-BHU          ");
textbackground(1);
textcolor(15);
gotoxy(1,2);
cprintf("I");
     for(i=2;i<80;i++)   {
     gotoxy(i,2);
     cprintf("M");
     gotoxy(i,23);
     cprintf("M");
     }
gotoxy(80,2);
cprintf(";");
for(x=3;x<23;x++)   {
gotoxy(1,x);
cprintf(":");
gotoxy(80,x);
cprintf(":");
for(i=2;i<80;i++)    {
gotoxy(i,x);
cprintf(" ");
}
}
gotoxy(80,23);
cprintf("<");
gotoxy(1,23);
cprintf("H");
gotoxy(7,4);
textcolor(14);
cprintf("enter the specimen number:  ");
cscanf("%d",&rabn);
gotoxy(7,4);
cprintf("specimen number is %d    ",rabn);
gotoxy(45,4);
textcolor(14);
cprintf("type-  ");
cscanf("%s",wd);
gotoxy(45,4);
textcolor(14);
cprintf("enter the file name          ");
cscanf("%s",dataf1);
gotoxy(7,6);
cprintf("file name           %s           ",dataf1);

fp=fopen(dataf1,"r+");
for(j=1;j<100;j++)  {
    fscanf(fp,"%d",&a);
    sum1+=a;
    }
  fclose(fp);
  fp=fopen(dataf1,"r+");
      for(j=1;j<700;j++) {
      fscanf(fp,"%d",&a);
      if(a>max1)   {
      max1=a;
      cpeaktime=j;
      }
      fclose(fp);
      gotoxy(7,8);
      cprintf("noiselevel=%d    db\n",sum1/100);
      gotoxy(40,8);
      cprintf("peak amplitude is %d  db\n",max1-(sum1/100));
      textbackground(1);
      textcolor(14);
      gotoxy(7,10);
      cprintf("enter the threshold value   (for SWF calculation)in db=");
      cscanf("%d",&threshold);
      gotoxy(7,12);
      cprintf("enter the gatewidth value(for SWF calculation)=  ");
      cscanf("%d",&b);
      gotoxy(7,12);
      cprintf("                               ");
      threshold=threshold-(sum1/100);
      gotoxy(7,10);
      cprintf("threshold is    =   %d     db gatewidth is %d",threshold,b);
      getch();
      fp=fopen(dataf1,"r+");
      for(j=1;j<b;j++)  {
      fscanf(fp,"%d",&a);
      do  {
      j++;
      fscanf(fp,"%d",&a);
      if(a>lpeak)
         lpeak=a;
      } while(a>0);
      if(lpeak>threshold)   {
             ccount++;
             clthtime=j;
             if(ccount==1)
                cfthtime=j;
      }
      lpeak=0;
 }
 th1=(sum1/100)+2;
 th2=th1+2;
 while(th2<max1)   {
 fp=fopen(dataf1,"f+");
 for(j=1;j<800;j++)  {
   fscanf(fp,"%d",&a);
   do   {
   j++;
   fscanf(fp,"%d",&a);
   if(a>lpeak)
      lpeak=a;
   }while(a>0);
   if(lpeak>th1)  {
          n1++;
          if(lpeak>th2)
              n2++;

          }
          lpeak  = 0;
   }
   cenergy1=((n1-n2)*((th1*th1)+(th2*th2))/10);
   cenergy=(cenergy+cenergy1);
   fclose(fp);
   n1=0;
   n2=0;
   th1+=2;
   th2+=2;
   }
   lpeak=0;
   crisetime=(cpeaktime-cfthtime);
   cduration=(clthtime-cfthtime);
   fclose(fp);

   textbackground(13);
   textcolor(15);
   gotoxy(7,13);
   cprintf("         parameter estimation ");
   textcolor(4);
   textbackground(7);
   for(x=14;x<22;x++) {
   for(i=7;i<74;i++)  {
   gotoxy(i,x);
   cprintf("  ");
   }
   }
   gotoxy(15,15);
   cprintf("stress wave factor is %d\n",ccount*10);
   gotoxy(15,16);
   cprintf("risetime is %d micro sec \n",crisetime);
   gotoxy(15,17);
   cprintf("duration is %d microsecs\n",cduration);
   gotoxy(15,18);
   cprintf("time of  flight is %d microsecs\n",cfthtime);
   gotoxy(15,19);
   cprintf("peak voltage time is%d micro secs\n",cpeaktime);
   gotoxy(15,20);
   cprintf("energy content of the wave is %10.2f\n",cenergy);
   getch();
   initgraph(&graphdriver,&graphmode," ");
   setcolor(10);
   setbkcolor(13);
   rectangle(1,1,639,199);
   rectangle(5,3,634,197);
   rectangle(5,25,634,197);
   rectangle(5,180,634,197);
   setviewport(1,1,640,200,1);
   settextstyle(1,0,2);
   outtextxy(120,1,   "acousto-ultrasonic signal processing   ");
   settextstyle(0,0,1);
   rectangle(345,48,605,160);
   i1=345;
   fp=fopen(dataf1,"r+");
   for(j1=1;j<750;j1++)  {
        for(k1=1;k1<3;k1++) {
        fscanf(fp,"%d",&a1);
        if(max1>=80)
            a1=a1/4;
        else if (80>=max1<50)
            a1=a1/3;
        else if(max1<50)
            a1=a1;
        y1=(105-a1);
        putpixel(i1,y1,13);
        if(k1==1&j1>2)
             line(i1-1,y2,i1,y1);
         fscanf(fp,"%d",&b1);
         if (max1>=80)
         b1=b1/4;
         else if(80>=max1<50)
           b1=b1/3;
         else if(max1<50)
           b1=b1;
         y2=(105-b1);
         putpixel(i1,y2,13);
         line(i1,y1,i1,y2);
         j1++;
         }
         i1++;
         }
        fclose(fp);
        getch();
        window(1,1,80,25);
        gotoxy(10,5);
        printf("specimen no:%d",rabn);
        gotoxy(10,6);
        printf("type-%s",wd);
        gotoxy(10,7);
        printf("file name-%s",dataf1);
        gotoxy(10,8);
        printf("noise level-%d db\n",sum1/100);
        gotoxy(10,9);
        printf("PA-%d db",max1-(sum1/100));
        gotoxy(10,10);
        printf("threshold- %d db",threshold);
        gotoxy(10,11);
        printf("gatewidth- %d msec.",b);
        gotoxy(10,15);
        printf("estimated perameters");
        gotoxy(10,17);
        printf("SWF %d\n",ccount*10);
        gotoxy(10,18);
        printf("risetime %d mirosecs\n",crisetime);
        gotoxy(10,19);
        printf("duration %d microsecs\n",cduration);
        gotoxy(10,20);
        printf("time of flight %d microsecs \n",cfthtime);
        gotoxy(10,21);
        printf("peak voltage time %d micro secs \n",cpeaktime);
        gotoxy(10,22);
        printf("energy content of wave %10.2f\n",cenergy);
        gotoxy(15,24);
        printf("school of bio-medical engineering,IT-BHU");

        }
getch();
}

0
1/21/2009 10:23:02 AM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
1073 Views

Similar Articles

[PageSpeed] 2

On Wed, 21 Jan 2009 05:23:02 -0500, kaushik <kaushikv318@gmail.com> wrote:

> hi am kaushik i would like to know if this code can be directly  
> converted to matlab.

Not directly, but it seems possible.
It might be difficult to follow the logic without any comments in the C  
code, but the operations and graphics appear easy enough to replicate in  
MATLAB.

Try walking through the code and understand the operations being done,  
maybe write down the steps (the code is small enough). To help you get  
started, here are some basic equivalent commands (you can ignore all the  
graphics command in teh C code for the first attempt).

cprintf, printf->DISP, TEXT
scanf->INPUT
fopen,fscanf, for, while, if -> same keywords
All the math operations are the same (+,-..etc). Though you can do better  
in MATAB using functions like SUM to replace a loop in C to compute the  
sum of an array.
Note: +=, ++ wont work, you would have to manually assign the new value to  
the variable:
var+=2 -> var=var+2
var++  -> var=var+1

Use 'doc <keyword>' to look up the help in MATLAB for the corresponding  
keywords, this should give you enough information about the syntax.
0
1/21/2009 2:11:07 PM
"kaushik" <kaushikv318@gmail.com> writes:

> hi am kaushik i would like to know if this code can be directly
> converted to matlab. if not what can is to be done to run it
> matlab. ma posting the code here. please reply as soon as possible

[trimmed C code]

This C code is mostly about getting user input and displaying the
results.  In MATLAB, you can probably skip all of that.  The
computations look very simple.

I would write a function that takes a struct of the input parameters,
does the simple calculations, and outputs a struct of the output
parameters (including the input?).  Don't do the text input.  Really.
If someone wants a nice interface to the function, write a MATLAB gui to
do it.  But start with the computation function.  It might be
sufficient.  Simply displaying the output structure might label
everything well enough.

Here's a start:

Command window interface:
------------------------------

data.specimen_number = 27;
data.filename = 'some_file';

output = calc_ultrasonic_stats(data);





calc_ultrasonic_stats.m:
--------------------------------------

function output = calc_ultrasonic_stats(data)

output = data;  % Start with a copy of the input

% Read data into wavedata using fopen/fscanf/fclose
% <You do this part>
% <It would be more "MATLAB-y" to pass in the actual data vector rather
% than the filename.  That would allow you to plot, to generate a set of
% input data and loop over this stats function for an entire set of
% inputs, etc.

% Example of the first few computations.
sum1 = sum(wavedata(1:100);
[max1 cpeaktime] = max(wavedata(1:700));

% Example of how to output your results
output.noiselevel = sum1 / 100;

% etc...
0
boettcher (2304)
1/21/2009 2:29:04 PM
kaushik wrote:
> hi am kaushik i would like to know if this code can be directly converted to matlab.

> gotoxy(1,1);

Sorry, no, there is no way to replicate gotoxy() in Matlab. gotoxy() is for moving
to a specific position on a -text- screen, and Matlab's text console does not
support addressable positioning.
0
roberson (2980)
1/21/2009 4:30:12 PM
Hi, I have the same problem that Kaushik, because i want to my program if have any mistake that i don t like, i thought that it back it self to another line, but in he same way rthat u express matlab don t have this utility, but i like to ask, if i can put a conditional, that could carry me to an instruction that i put before...

If a<1
%total is a file .m that i make, is a functionexactly that is used after that.
 total
else
%parameter is other .m function that i caller before, 
parameter
end
 
 the problem or mistake that matlab give me is a eternal loop, if i dont send the correct data, it send me to "parameter" and just stay there.

Walter Roberson <roberson@hushmail.com> wrote in message <74Idl.116988$H12.64429@newsfe12.iad>...
> kaushik wrote:
> > hi am kaushik i would like to know if this code can be directly converted to matlab.
> 
> > gotoxy(1,1);
> 
> Sorry, no, there is no way to replicate gotoxy() in Matlab. gotoxy() is for moving
> to a specific position on a -text- screen, and Matlab's text console does not
> support addressable positioning.

Hi
0
11/15/2009 6:00:04 AM
Dear Walter!

> > hi am kaushik i would like to know if this code can be directly converted to matlab.
> 
> > gotoxy(1,1);
> 
> Sorry, no, there is no way to replicate gotoxy() in Matlab. gotoxy() is for moving
> to a specific position on a -text- screen, and Matlab's text console does not
> support addressable positioning.

gotoxy(1,1) works like CLC.
A more general gotoxy can be simulated also by storing a CHAR matrix persistently, using EVALC to encapsulate all DISP calls and insert the output in the CHAR matrix, which is written to the command window after CLC.

Nevertheless, this is surely a mess and not "directly converted".

Another idea would be a simulated terminal, which displayes text in a UICONTROL. DISP can be redirected there with a similar trick as above. But instead of investing (wasting) time in creating old fashioned text interfaces, a modern graphical user interface is really better...

I cannot imagine the benefit of converting a working C-program to Matlab.

Jan
0
11/15/2009 3:23:01 PM
Reply: