Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.30 KB | None | 0 0
  1. //Lukasz Swierczek 213431
  2.  
  3. //Zalaczmy blioteki
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #define _USE_MATH_DEFINES
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <time.h>
  10.  
  11.                         //Prototypy funkcji:
  12. void sortowanie();      //ktora sortuje wartosci wpisane do tabp z s.taby
  13. void zamien();          //zamienia wartosci dwoch zmiennych
  14. void wykres();          //generuje kod html z wykresem
  15. void wybor();           //ktora zadaje pytanie jaki wykres chcemy zobaczyc
  16. void akcje();           //generujacej
  17. void wybor2();          //
  18. void akcje2();          //
  19. void zaszumianie();     //
  20. void odszumianie();     //
  21. void wykres_S7();       //
  22. void wykres_S12();      //
  23. double szum();          //
  24.  
  25. struct sygnal //Delkaracja zmienntych struktury
  26.     {
  27.     int lp;
  28.     int fp;
  29.     int probki;
  30.     int czy_byl_szum;
  31.     double amp;
  32.     double fs;
  33.     double ts;
  34.     double *tabx;
  35.     double *taby;
  36.     };
  37.  
  38.  
  39. int main()
  40.     {
  41.     struct sygnal s;
  42.     char rs, co;
  43.     int i;
  44.     int powtorzenie=0;
  45.  
  46.     s.amp=0;
  47.     s.fs=0;
  48.     s.fp=0;
  49.     s.ts=0;
  50.     s.czy_byl_szum=0;
  51.     s.probki=0;
  52.  
  53.     printf("Jakiego rodzaju chcesz wybrac sygnal, lub co chcesz zrobić:\n");
  54.     printf("a)Sygnal liniowy S7\n");
  55.     printf("b)Sygnal cosinusoidalny S12\n");
  56.     printf("c)Wylaczyc program\n");
  57.     scanf(" %c",&rs);
  58.  
  59.     switch(rs)
  60.         {
  61.         case 'a':
  62.             printf("Wybrales sygnal o przebiegu S7. \n");
  63.             break;
  64.  
  65.         case 'b':
  66.             printf("Wybrales sygnal o przebiegu S12. \n");
  67.             break;
  68.  
  69.         case 'c':
  70.             free;
  71.             return 0;
  72.  
  73.         default :
  74.             system("cls");
  75.             printf("Wybrales niewlasciwa litere! \n\n");
  76.             main();
  77.             return 0;
  78.         }
  79.  
  80.     printf("Podaj wartosc ampiltudy dla tego sygnalu.\n");
  81.     scanf("%lg",&s.amp);
  82.     printf("Podaj wartosc czestotliwosci sygnalu.\n");
  83.     scanf("%lg",&s.fs);
  84.     printf("Podaj wartosc czasu trwania sygnalu.\n");
  85.     scanf("%lg",&s.ts);
  86.     printf("Podaj wartosc czestosci probkowania.\n");
  87.     scanf("%d",&s.fp);
  88.    
  89.     printf("\nA=%lg \nfs=%lg \nts=%lg \nfp=%d\n\n", s.amp, s.fs, s.ts, s.fp);
  90.    
  91.     s.lp = (int)s.ts*s.fp+1;
  92.    
  93.     s.tabx = (double*)malloc(s.lp*sizeof(double));
  94.     s.taby = (double*)malloc(s.lp*sizeof(double));
  95.    
  96.     switch(rs)
  97.         {
  98.         case 'a':
  99.             wykres_S7(s.amp, s.fs, s.ts, s.fp, s.lp, s.tabx, s.taby);
  100.             wybor(s.czy_byl_szum);
  101.             scanf(" %c",&co);
  102.             akcje(co, s.tabx, s.taby, s.lp, s.czy_byl_szum);
  103.             break;
  104.            
  105.         case 'b':
  106.             wykres_S12(s.amp, s.fs, s.fp, s.lp, s.tabx, s.taby);
  107.             wybor(s.czy_byl_szum);
  108.             scanf(" %c",&co);
  109.             akcje(co, s.tabx, s.taby, s.lp, s.czy_byl_szum);
  110.             break;
  111.         }
  112.        
  113.     }
  114.    
  115. void wykres_S7(double amp, double fs, double ts, int fp, int lp, double *tabx, double *taby)
  116.     {
  117.     int i,powt;
  118.    
  119.     for(i=0;i<lp;i++)
  120.         {          
  121.         tabx[i]=(double)i/fp;
  122.         powt = tabx[i]/(1/fs);
  123.  
  124.         if(tabx[i] >= (0 + (powt*(1/fs))) && tabx[i]<((1/(4*fs))+(powt*(1/fs))))
  125.             taby[i]=(tabx[i]-(powt*(1/fs)))*(amp*fs*4);
  126.         if(tabx[i] >= ((1/(4*fs))+(powt*(1/fs))) && tabx[i]<=((1/(2*fs))+(powt*(1/fs))))
  127.             taby[i]=((-1)*(tabx[i]-(powt*(1/fs)))*(amp*fs*4))+(2*amp);
  128.         if(tabx[i] > ((1/(2*fs))+(powt*(1/fs))) && tabx[i]<=((1/fs)+(powt*(1/fs))))
  129.             taby[i]=(-1)*amp;
  130.            
  131.         printf("f(%lg)=%lg\n",tabx[i],taby[i]);
  132.         }
  133.     }
  134.    
  135. void wykres_S12(double amp, double fs, int fp, int lp, double *tabx, double *taby)
  136.     {
  137.     int i,powt;
  138.    
  139.     for(i=0;i<lp;i++)
  140.         {          
  141.         tabx[i]=(double)i/fp;
  142.         taby[i]=amp*cos(tabx[i]*(2*M_PI*fs));
  143.            
  144.         printf("f(%lg)=%lg\n",tabx[i],taby[i]);
  145.         }
  146.     }
  147.  
  148. double szum()
  149.     {
  150.     double z=0;
  151.     int i;
  152.  
  153.     for(i=0;i<12;i++)
  154.         z+=rand() % 101;
  155.  
  156.     z=z/1200;
  157.     return z;
  158.     }
  159.    
  160. void zaszumianie(int lp, double *taby)
  161.     {
  162.     int i;
  163.    
  164.     for(i=0;i<lp;i++)
  165.         taby[i]=taby[i]+szum();
  166.     }
  167.    
  168. void wybor(int czy_byl_szum)
  169.     {
  170.     printf("\n");
  171.     printf("Jaki wykres chcesz zobaczyc:\n");
  172.     printf("czy_byl_szum = %d\n",czy_byl_szum);
  173.     if(czy_byl_szum==0)
  174.         printf("a)Nie zaszumiony\n");
  175.     printf("b)Zaszumiony\n");
  176.     printf("c)Odszumiony\n");
  177.     printf("\n");
  178.     printf("d)Zakoncz program\n");
  179.     }
  180.  
  181. void akcje(int co, double *tabx, double *taby, int lp, int czy_byl_szum)
  182.     {
  183.     int i;
  184.     switch(co)
  185.         {
  186.         case 'a':
  187.             printf("czy_byl_szum = %d\n",czy_byl_szum);
  188.             if(czy_byl_szum==0)
  189.             {
  190.                 wykres(lp, tabx, taby, "nie_zaszumiony.html");
  191.                 wybor(czy_byl_szum);
  192.                 scanf(" %c",&co);
  193.                 akcje(co, tabx, taby, lp, czy_byl_szum);
  194.             }
  195.             else
  196.             {
  197.                 printf("Wybrales niewlasciwa litere! \n\n");
  198.                 wybor(czy_byl_szum);
  199.                 scanf(" %c",&co);
  200.                 akcje(co, tabx, taby, lp, czy_byl_szum);
  201.             }  
  202.             return 0;
  203.  
  204.         case 'b':
  205.             zaszumianie(lp, taby);
  206.             czy_byl_szum=1;
  207.             wykres(lp, tabx, taby, "zaszumiony.html");
  208.             wybor(czy_byl_szum);
  209.             scanf(" %c",&co);
  210.             akcje(co, tabx, taby, lp, czy_byl_szum);
  211.             return 0;
  212.  
  213.         case 'c':
  214.             do
  215.                 {
  216.                 printf("Podaj z ilu probek ma zostac przeprowadzone odszumiane.\n");
  217.                 scanf("%d",&i);
  218.                 if(lp<=i)
  219. //                  system("cls");
  220.                     printf("Niewlasciwa liczba probek!\n");
  221.                 }
  222.             while(lp<=i);
  223.             if(czy_byl_szum==0)
  224.                 zaszumianie(lp, taby);
  225.             odszumianie(tabx, taby, i, lp);
  226.             wybor(czy_byl_szum);
  227.             scanf(" %c",&co);
  228.             akcje(co, tabx, taby, lp, czy_byl_szum);
  229.             return 0;
  230.            
  231.         case 'd':
  232.             free;
  233.             return 0;  
  234.            
  235.         default :
  236. //          system("cls");
  237.             printf("Wybrales niewlasciwa litere! \n\n");
  238.             wybor(czy_byl_szum);
  239.             scanf(" %c",&co);
  240.             akcje(co, tabx, taby, lp, czy_byl_szum);
  241.             return 0;
  242.    
  243.         }
  244.     }
  245.     /*
  246. void wybor2()
  247.     {
  248.     printf("Jaki wykres chcesz zobaczyc:\n");
  249.     printf("a)Zaszumiony\n");
  250.     printf("b)Odszumiony\n");
  251.     printf("\n");
  252.     printf("c)Zakoncz program\n");
  253.     }
  254.    
  255. void akcje2(int co, double *tabx, double *taby, int lp)
  256.     {
  257.     int i;
  258.     switch(co)
  259.         {
  260.         case 'a':
  261. //          zaszumianie(lp, taby);
  262.             wykres(lp, tabx, taby, "zaszumiony.html");
  263.             wybor2();
  264.             scanf(" %c",&co);
  265.             akcje2(co, tabx, taby, lp);
  266.             return 0;
  267.  
  268.         case 'b':
  269.             do
  270.                 {
  271.                 printf("Podaj z ilu probek ma zostac przeprowadzone odszumiane.\n");
  272.                 scanf("%d",&i);
  273.                 if(lp<=i)
  274. //                  system("cls");
  275.                     printf("Niewlasciwa liczba probek!\n");
  276.                 }
  277.             while(lp<=i);
  278.             odszumianie(tabx, taby, i, lp);
  279.             wybor2();
  280.             scanf(" %c",&co);
  281.             akcje2(co, tabx, taby, lp);
  282.             return 0;
  283.            
  284.         case 'c':
  285.             break;
  286.             return 0;  
  287.            
  288.         default :
  289. //          system("cls");
  290.             printf("Wybrales niewlasciwa litere! \n\n");
  291.             wybor2();
  292.             scanf(" %c",&co);
  293.             akcje2(co, tabx, taby, lp);
  294.             return 0;
  295.    
  296.         }
  297.     }  
  298.     */
  299. void odszumianie(double *tabx, double *taby, int probki, int lp)
  300.     {
  301.     int i, j;
  302.     int z=0;
  303.     double *tabp;
  304.     double *tabm;
  305.  
  306.     z = lp - probki + 1;
  307.  
  308.     tabp = (double*)malloc(probki*sizeof(double));
  309.     tabm = (double*)malloc(z*sizeof(double));
  310.  
  311.         for(i=0;i<z;i++)
  312.         {
  313.             for(j=0;j<probki;j++)
  314.                 tabp[j]=taby[j+i];
  315.  
  316.             sortowanie(tabp, probki);
  317.  
  318.             tabm[i] = (tabp[(int)((probki-1)/2)] + tabp[probki - (int)((probki+1)/2)])/2;
  319.         }
  320.  
  321.     wykres(z, tabx, tabm, "odszumiony.html");
  322.    
  323.     }
  324.  
  325.  
  326. void sortowanie(double tab[], int k)
  327.     {
  328.     int i, j;
  329.     for (i = 0; i<k; i++)
  330.         for (j=0; j<k-1; j++)
  331.             if (tab[j] > tab[j+1])
  332.                 zamien(&tab[j], &tab[j+1]);      
  333.     }
  334.  
  335. void zamien(double *a, double *b)
  336.     {
  337.     double temp;
  338.  
  339.     temp = *b;
  340.     *b = *a;
  341.     *a = temp;
  342.     }
  343.    
  344. void wykres(int l, double *tabx, double *taby, char nazwa[])
  345.     {
  346.     FILE *fp;
  347.     int i;
  348.  
  349.     printf("Rysuj %s\n",nazwa);
  350.     fp = fopen(nazwa,"w");
  351.  
  352.     fprintf(fp,"<html>\n");
  353.     fprintf(fp,"<head>\n");
  354.     fprintf(fp,"<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>\n");
  355.     fprintf(fp,"<script type=\"text/javascript\">\n");
  356.     fprintf(fp,"google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});\n");
  357.     fprintf(fp,"google.setOnLoadCallback(drawChart);\n");
  358.     fprintf(fp,"function drawChart() {\n");
  359.     fprintf(fp,"var data = google.visualization.arrayToDataTable([\n");
  360.  
  361.     fprintf(fp,"['i', 'Wartosc'],\n");
  362.     for(i=0;i<l;i++)
  363.         {
  364.         fprintf(fp,"[%lg, %lg],\n",tabx[i],taby[i]);   //przekazanie danych na wykres
  365.         }
  366.     fprintf(fp,"[%lg, %lg]\n",tabx[i-1],taby[i-1]);
  367.     fprintf(fp,"]);\n");
  368.  
  369.     fprintf(fp,"var options = {\n");
  370.     fprintf(fp,"title: 'Wybrany sygnal'\n");
  371.     fprintf(fp,"};\n");
  372.  
  373.     fprintf(fp,"var chart = new google.visualization.LineChart(document.getElementById('chart_div'));\n");
  374.     fprintf(fp,"chart.draw(data, options);\n");
  375.     fprintf(fp,"}\n");
  376.     fprintf(fp,"</script>\n");
  377.     fprintf(fp,"</head>\n");
  378.     fprintf(fp,"<body>\n");
  379.     fprintf(fp,"<div id=\"chart_div\" style=\"width: 900px; height: 500px;\"></div>\n");
  380.     fprintf(fp,"</body>\n");
  381.     fprintf(fp,"</html>\n");
  382.  
  383.     fclose(fp);
  384.  
  385.     system(nazwa);      //uruchomienie pliku html
  386.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement