Hallo Forengemeinde.
ich hab ein Problem an dem ich jetzt schon ewig hänge und einfach nicht
weiterkomme.
Und zwar geht es um eine Hausübung.
Aufgabenstellung ist eine diskrete Cosinustansformation zu schreiben.
zuerst war das ganze in C gefragt und jetzt mit der C++ syntax
allerdings nicht objektorientiert.
Soweit so gut. Ich hab das ganze programmiert mit Codeblocks compiliert
aber ich bekomme andere Werte. sie sind zwar ähnlich von der Struktur,
aber eben falsch...
könnte mir bitte jemand kurz die zwei Funktionen vergleichen und mir auf
die Sprünge helfen? ich hab die beiden Projekte noch einmal komplett im
Anhang beigefügt für den Fall das es Fragen gibt.
hier der funktionierende Code
1 | #include "dct.h"
|
2 | #include <stdlib.h>
|
3 | #include <stdio.h>
|
4 | #include <math.h>
|
5 |
|
6 | // Function to compute the discrete cosine transform DCT-II of a
|
7 | // dataseries and store the result in another dataseries of equal size
|
8 | void dct_ii (dataseries p, dataseries q) {
|
9 | // Copy dataseries size to time saver, check for equal sizes
|
10 | int n = p.size;
|
11 | if (q.size != n) {
|
12 | fprintf (stderr, "dct_ii: dataseries sizes don't match, exiting\n");
|
13 | exit (-1);
|
14 | }
|
15 | // Perform actual DCT-II
|
16 | const double pi = 3.1415926535897932;
|
17 | for (int i=0; i<n; ++i) {
|
18 | q.value[i] = 0.0;
|
19 | for (int j=0; j<n; ++j) {
|
20 | q.value[i] = q.value[i] + cos (pi / n * i * (j + 0.5)) * p.value[j];
|
21 | }
|
22 | }
|
23 | return;
|
24 | }
|
und hier der mit falschen Ergebnissen.
1 | #include <iostream>
|
2 | #include <vector>
|
3 | #include <cstdio>
|
4 | #include <math.h>
|
5 |
|
6 | using namespace std;
|
7 |
|
8 | void dct(vector<double> &eingabe, vector<double> &ausgabe) // Berechnung der Cosinustransformation
|
9 | {
|
10 | int n = eingabe.size();
|
11 | if (ausgabe.size() != n) {
|
12 | cout<<"dct_ii: dataseries sizes don't match, exiting"<<endl;
|
13 | return;
|
14 | }
|
15 | // Perform actual DCT-II
|
16 | const double pi = 3.1415926535897932;
|
17 | for (int i=0; i<n; ++i) {
|
18 | ausgabe[i] = 0.0;
|
19 | for (int j=0; j<n; ++j) {
|
20 | ausgabe[i] = ausgabe[i] + cos (pi / n * i * (j + 0.5)) * eingabe[j];
|
21 |
|
22 | }
|
23 | }
|
24 | return;
|
25 | }
|
Danke im Vorraus!
Marian