Transfer function to a Difference equation

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi,

There are a ton of documents online that talk about C functions and syntax and all that. For complex math i found this first try:
http://en.cppreference.com/w/c/numeric/complex

I actually never used complex number math in C using the standard include files, i always made my own number crunchers for both real and complex arguments, but this looks interesting so i might try it too maybe tomorrow some time. I'll keep it very very simple and post results here also.
Console applications are very easy to make in C. For Windows applications you have to study the Windows API which would take too long for you to get ready by next month.

Here is a sample program in C. Note function calls may be different for other compilers or include files.


#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <dos.h> //for sleep()


void main(void)
{
complex xx,yy;
double y;

xx=Complex(1.0,2.0);
yy=cos(xx);
printf("%f,%f\n",real(xx),imag(xx));
printf("%f,%f\n\n",real(yy),imag(yy));

xx=acos(yy);
printf("%f,%f\n",real(yy),imag(yy));
printf("%f,%f\n\n",real(xx),imag(xx));

xx=Complex(2.0,2.0);
yy=atan(xx);
printf("%f,%f\n",real(xx),imag(xx));
printf("%f,%f\n\n",real(yy),imag(yy));

y=atan2(2.0,2.0);
printf("%1.8f\n",y);

sleep(5);
}

The above prints the following to a console screen:

1.000000,2.000000
2.032723,-3.051898

2.032723,-3.051898
1.000000,2.000000

2.000000,2.000000
1.311223,0.238878

0.78539816
Hi Sir. Thank you so very much. I really appreciate your help and guidance.
I'm going to try all of this and post the results. I was busy with math today (Ztransform and Matrix equations) so tomorrow will definitely do DSP programmes.
Thank you.
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hello Sir,
I trust that you are well.
So sorry for delayed response was so busy with maths and work.

I am not able to run EUPHORIA, I have the EUPHORIA folder on my C drive. But when I try to compile a file i saved in that folder it tells me unable toe open file.

Today I download code blocks, an open source IDE and cygwin and MGwin which are C compilers.The work seems to work but am not able to compile complex functions.

Do you know the steps I can take to sort out EUPHORIA or should I stick with the other C compilers.

Thank you Sir.
 

MrAl

Joined Jun 17, 2014
11,480
Hi,

You dont Compile Euphoria. You just run the file.
Files that you run end with either .ex, or .exw so look for those types.
If you want to run an .exw file for example, you can right click on it and click "open with" and then navigate to the Euphoria/bin directory, then select Exw.exe, then 'open' or whatever. Exw.exe runs the file say "MyFile.exw" and that file has your own code in it.
Exw.exe is an interpreter, not a compiler. Files like "MyFile.exw" can be modified in a regular text editor and then run immediately without having to compile.

If you get an error trying to run a file like "MyFile.exw" you would have to copy and paste that error here so i could see what it is. Your environment variables have to include the search path "Euphoria/Include" or some files wont run if they need include files from that directory, unless you explicitly state them in the file like this:
include misc.e

and that line includes various functions that you can call which make writing the programs easier. There are many other include files, but you can get to them later.

A very short program you can try is this:

include misc.e
?123
sleep(10)

and save that as a text file but with extension ".exw" so for example
MyFirstFile.exw

That program will print the number 123 and then wait 10 seconds and then close out.

If you get an error, then try this:

include C:\Euphoria\include\misc.e
?123
sleep(10)

If that second file works and the first doesnt, then your environment variables have to be modified slightly to include the search path Euphoria\Include

See what happens. Use a regular text editor to write those files, like Notepad. Dont use a text editor that adds format info to the file like Wordpad or something like that.

Let me know what happens :)
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi Sir, WOW hooray it works

include misc.e
?123
sleep(10)

saved it as test.exw in the EUPHORIA folder it the command prompt opens and shows "123"

this means I wasted all my data downloading that cygwin compiler :-(

I should have asked first before downloading it.

thank you Sir.

Let try the other programs that you previously provided me.

thanks
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi Sir,
Sorry to trouble you again.

I tried the below program and I am getting an error.

include <math.e> //or whatever is available
double t,y,dt,w,A;
int k;

A=10; //amplitude
dt=0.1; //time step
w=2*pi*5;
for (k=0;k<63;k++)
{
t=dt*(double)k;
y=A*sin(w*t);
//plot points (t,y) with some function
}


upload_2016-7-30_22-5-58.png

I tried searching the EUPHORIA folder for a math library but I can not find one.
what can I used?

thank you.
 

MrAl

Joined Jun 17, 2014
11,480
Hello again,

There is no math.e this is different. It automatically does math of certain levels.

For example, type:
atom x,y

x=2
y=3
?x*y

prints "6"

Example:

atom x,y,z

x=2
y=3

z=x+y

?z

prints "5"

There is no double, it's all double when you declare as "atom".

atom a,b,c, def, g,k,z

they are all double now.

There are built in functions such as
power(x,y)

There is no exp(x) but you can write your own quite easily:
function exp(atom x)
return power(2.7182818284590,x)
end function

We can get into functions later if you prefer.
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi Sir,
Thanks very much. Let me try this and see what I get.

I wanted to know but at the same time not to jump the gun and go ahead too far.

I see a similar question to below always appearing in our exams. Is it possible to do this type of question?

upload_2016-7-31_15-3-36.png

I have a basic understanding on analogue to digital converters from studying circuit analysis. But is it possible to simulate an A/D converter?

Thank you Sir.
 

MrAl

Joined Jun 17, 2014
11,480
Hi again,

Sure. But let me ask you one important question first.

Will any of your exams allow you to use a sort of less known language like Euphoria or do you have to use some mainstream language like C or C++ ?
This is rather important so you should check to make sure before we go too far with that language. Alternately, we can work in both C and Euphoria, doing examples in both so we get the hang of both methods.
I guess i wonder when you have an exam who brings the include files, or can you assume you have some include files.

To simulate an AD converter you just have to round your samples to values that coincide with the allowable steps given the number of bits you want to simulate.
An ADC takes in real world values of assumed infinite precision and outputs those values in the form of a finite precision count.
So for example if we had a 0.005v input and the ADC resolution was 10 bit and the supply reference voltage was 5v, we would get a count of 1 on the output. If the input was 0.010v then the output would be 2, and if the input was 0.015v then the output would be 3,etc., up to a count of 1023.
Values that do not exactly match an integer multiple of the step resolution (0.005v in these examples) get rounded down. So a voltage input of 0.011v would still output a count of 2, and that goes all the way up to 0.014999 which still would give an output of 2 unless you want to simulate in a little noise which could kick it up above 0.015 now and then. In fact, noise becomes an important part of the simulation for some types of applications like oversampling.
So for the most basic 10 bit ADC simulation with reference voltage 5.120 volts you could do this:
ADC=floor(Vin/0.005)

and there you have it. For example for an input of 2v we get: ADC=400 and that is the count. To convert that back into voltage we get 400*0.005=2 volts.
Note that the rounding mechanism means that an input of 2.001v still gives a count of 400 and also converts to 2 volts again. That is because of the limited resolution.

There are other things you can add such as noise and non linearity.

I happened to notice that you pick this stuff up quick so you should have no problem with this stuff, it will be a breeze for you :)
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi Sir,

Thank you again for the time taking in helping me.

I did email the lecturer that is in-charge of this subject and the practical exam. He has heard of Euphoria but does not think it will be installed on PC's that we will be using for the exam. But he said definitely C compilers and IDE's are there and also matlab and other software that he gave us on the list.

Thanks sir for having faith in me. I hope can pull this off by end of this month :)

So when you say samples, do you mean we take some made up values?
Or do we take a random analogue signal?

thank you.
 

MrAl

Joined Jun 17, 2014
11,480
Hi,

Ok we will use C then.

Dont know what you mean by we make up values. Why would we make up values?
You mean to simulate an input waveform? We might do that yes.
For example, if we make input sin(t) then we would see the ADC make an output that looks like a stepped sine wave because it rounds each value to the next lowest sample.

Oh sample as in sampling a waveform. When we sample a waveform we take readings every so often, like every 1 second, and store those values perhaps.
This might be faster, like every 10us and store those or use those in some way.
The ADC would take 'samples' of a sine wave for example.
So for sin(t) at t=0 the sample would be 0v, and at t=pi/2 the sample would be 1v, then at t=pi the sample would be 0v again.
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi,

Ok we will use C then.

Dont know what you mean by we make up values. Why would we make up values?
You mean to simulate an input waveform? We might do that yes.
For example, if we make input sin(t) then we would see the ADC make an output that looks like a stepped sine wave because it rounds each value to the next lowest sample.

Oh sample as in sampling a waveform. When we sample a waveform we take readings every so often, like every 1 second, and store those values perhaps.
This might be faster, like every 10us and store those or use those in some way.
The ADC would take 'samples' of a sine wave for example.
So for sin(t) at t=0 the sample would be 0v, and at t=pi/2 the sample would be 1v, then at t=pi the sample would be 0v again.
Hi Sir,
I meant to say "simulate an input waveform"

C is perfect for me thank you.

I appreciate your help.
 

MrAl

Joined Jun 17, 2014
11,480
Hi again,

Oh ok good, then i guess sin(t) would be an example of generating an input, and interestingly a finite length Fourier series could also be used as an input. This gives rise to another way to analyze circuits too :)
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi again,

Oh ok good, then i guess sin(t) would be an example of generating an input, and interestingly a finite length Fourier series could also be used as an input. This gives rise to another way to analyze circuits too :)
Awesome Sir,
When can we start. Must I research on how to write a program from a sin(t) input?

thank you sir.
 

MrAl

Joined Jun 17, 2014
11,480
Hi,


You can use any signal you want, that is just one example.

Here is a quick example of some basic math.
Get this working first and we can go from there.
With my compiler i had to use a .cpp file in order to use
complex math, i cant use a .c file. Yours may be different.

Code:
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <dos.h> //for sleep()

void main(void)
{
  complex xx,yy;
  double pi;

  pi=3.1415926535897932384626433832795; //pi doesnt seem to be defined in math.h

  xx=Complex(1.0,2.0); //create a complex number 1+2*j
  yy=cos(xx); //calculate the cos
  printf("%f,%f\n",real(xx),imag(xx));  //print the real and imag parts of xx
  printf("%f,%f\n\n",real(yy),imag(yy)); //print the real and imag parts of yy

  xx=acos(yy); //find the inverse cos of yy which was the cos of xx.
  printf("%f,%f\n",real(yy),imag(yy)); //print again
  printf("%f,%f\n\n",real(xx),imag(xx)); //print again, should be 1,2 again.


  sleep(55); //leave the console open for 55 seconds, then close out.
}
 
Last edited:

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi,


You can use any signal you want, that is just one example.

Here is a quick example of some basic math.
Get this working first and we can go from there.
With my compiler i had to use a .cpp file in order to use
complex math, i cant use a .c file. Yours may be different.

Code:
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <stdio.h>
#include <dos.h> //for sleep()

void main(void)
{
  complex xx,yy;
  double pi;

  pi=3.1415926535897932384626433832795; //pi doesnt seem to be defined in math.h

  xx=Complex(1.0,2.0); //create a complex number 1+2*j
  yy=cos(xx); //calculate the cos
  printf("%f,%f\n",real(xx),imag(xx));  //print the real and imag parts of xx
  printf("%f,%f\n\n",real(yy),imag(yy)); //print the real and imag parts of yy

  xx=acos(yy); //find the inverse cos of yy which was the cos of xx.
  printf("%f,%f\n",real(yy),imag(yy)); //print again
  printf("%f,%f\n\n",real(xx),imag(xx)); //print again, should be 1,2 again.


  sleep(55); //leave the console open for 55 seconds, then close out.
}
Hi Sir, Thanks a Million for your time.

I tried the above code but I get errors even when I try to save the file as a .cpp file.

upload_2016-8-2_17-42-18.png

I think its might be related to my compiler.

I am using the GNU Gcc compiler. I can download the compiler that you have.

thank you.
 

MrAl

Joined Jun 17, 2014
11,480
Hi,

If you use a text editor to type the code in you can save it as .txt, then change the extension to .cpp That should work but try it. You also probably have to set up your system to be able to open .cpp files with a text editor.
Does your compiler allow C++ too or just C ?

You can also try this with the .c file:
double pi;
pi=3.14159;

In other words, get rid of all the references to complex functions and see if it compiles.
If it does, then you'll have to figure out why, perhaps from the documentation that came with your compiler. But try that txt to cpp file rename and see if that works first.

Compilers are often difference so we have to find out what yours likes and doesnt like :)

For example, some compilers do not like:
void main(void)

they want:
int main(void)

and a return value of something, like zero 0.

int main(void)
{
body of code
return 0;
}
 

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi,

If you use a text editor to type the code in you can save it as .txt, then change the extension to .cpp That should work but try it. You also probably have to set up your system to be able to open .cpp files with a text editor.
Does your compiler allow C++ too or just C ?

You can also try this with the .c file:
double pi;
pi=3.14159;

In other words, get rid of all the references to complex functions and see if it compiles.
If it does, then you'll have to figure out why, perhaps from the documentation that came with your compiler. But try that txt to cpp file rename and see if that works first.

Compilers are often difference so we have to find out what yours likes and doesnt like :)

For example, some compilers do not like:
void main(void)

they want:
int main(void)

and a return value of something, like zero 0.

int main(void)
{
body of code
return 0;
}
Hi Sir, Thanks for the tips it managed to finally work.

upload_2016-8-3_10-59-39.png

Thank you :)
 

MrAl

Joined Jun 17, 2014
11,480
Hi again,

That's good news :)

I am happy you got it working, however, read the note i put in there and recall we talked about testing a function by using the inverse function and in many cases we SHOULD get the original result back again.
So in brief, if we input 1.2 and we get 3.4 out, then using the inverse function we should get 1.2 back again. This may not be true for all functions, but often it does work. In teh case of cos(r,i) and acos(r,i) it should work. In other words, in your last line of your console you had better get a result of 1.0000, 2.0000 or else you have to find out why it didnt work. It looks like in your program it is only operating on the real parts, thus the complex lib looks like it is not actually taking part in the calculation, which could be because you are not using a .cpp file. You'll have to check into that a little.

BTW, did you have to use creal() and cimag() instead of just real() and imag()?

You can check that out first and try this next test later or try this next test first.

Add these to the end of your current program:

Code:
  double t,dt,w,A,y,ADC; //maybe put this part at the start of the program with the other variable declarations.

  printf(" \n");

  dt=0.125;
  w=2*pi*1; //frequency is 1Hz
  A=5;
  for(int k=0;k<=8;k++)
  {
    t=k*dt;
    y=A*sin(w*t);

    //if y is less than zero we dont want to send it to the ADC:
    if (y>=0)
      ADC=floor(y/0.005);
    else
      ADC=0;
    printf("%f  %+f  %6.1f\n",t,y,ADC);
  }

(then return 0; that you already have in there)
 
Last edited:

Thread Starter

naickej4

Joined Jul 12, 2015
206
Hi again,

That's good news :)

I am happy you got it working, however, read the note i put in there and recall we talked about testing a function by using the inverse function and in many cases we SHOULD get the original result back again.
So in brief, if we input 1.2 and we get 3.4 out, then using the inverse function we should get 1.2 back again. This may not be true for all functions, but often it does work. In teh case of cos(r,i) and acos(r,i) it should work. In other words, in your last line of your console you had better get a result of 1.0000, 2.0000 or else you have to find out why it didnt work. It looks like in your program it is only operating on the real parts, thus the complex lib looks like it is not actually taking part in the calculation, which could be because you are not using a .cpp file. You'll have to check into that a little.

BTW, did you have to use creal() and cimag() instead of just real() and imag()?

You can check that out first and try this next test later or try this next test first.

Add these to the end of your current program:

Code:
  double t,dt,w,A,y,ADC; //maybe put this part at the start of the program with the other variable declarations.

  printf(" \n");

  dt=0.125;
  w=2*pi*1; //frequency is 1Hz
  A=5;
  for(int k=0;k<=8;k++)
  {
    t=k*dt;
    y=A*sin(w*t);

    //if y is less than zero we dont want to send it to the ADC:
    if (y>=0)
      ADC=floor(y/0.005);
    else
      ADC=0;
    printf("%f  %+f  %6.1f\n",t,y,ADC);
  }

(then return 0; that you already have in there)
Hi Sir,
Thank you once again.

I tried to add the code at the end of the current code it complains about "w=2*pi*1;"
Gives an error about C99 mode.
I really think I need to download another compiler or not sure how to use these function.

I even changed the file to ".cpp" still same issues.

I came to work and a friend has redhat linux OS with g++ compiler for C/C++, he also tried it giving the same problem.

thank you.
 

MrAl

Joined Jun 17, 2014
11,480
Hi,

Oh that's too bad, not sure what would cause it not to be able to do simple math.

You can try this:
w=2.0*pi*1.0;

see if that helps. If not try this:
w=6.28318530718;


The idea when you get an error, if you want to try, it so change whatever the error is and see if you can get it to work. It's good to learn how to troubleshoot these things too.

I also read that you may have to include a file tgmath.h:
#include <tgmath.h>
 
Last edited:
Top