我试图用指针在C中实现一个卷积算法。
我知道我的反密码是正确的。然而,我很难调用函数的主要目的,以获得预期的结果。任何帮助都是非常感谢的。
// deconvolution.c
#include "deconvolution.h"
#include "math.h"
void deconvolution (double *Win, double *Vin, int *N, int *j, int *L, double *ht, double *gt, double *Vout)
{
int k, n, t;
for(t = 0; t < *N; t++) {
k = t;
Vout[t] = (ht[0] * Win[k]) + (gt[0] * Vin[k]);
for(n = 1; n < *L; n++) {
k += (int) pow(2.0, (double) *j - 1.0);
if(k >= *N) k -= *N;
Vout[t] += (ht[n] * Win[k]) + (gt[n] * Vin[k]);
}
}
}
//////////////
// deconvolution.h
#include <stdio.h>
void deconvolution (
double *Win, double *Vin, int *N, int *j, int *L, double *ht, double *gt, double *Vout);
//////////////
// main.c
#include <stdio.h>
int main(int argc, const char * argv[]) {
int N = 9; // Size of Win and Vin
int J = 3; // Levels
int L = 4; // Size of gt and ht
double *Vout = NULL; // output will be stored here
double Win = {1.0, -2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, -9.0};
double Vin = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, -7.0, 8.0, 9.0};
double ht = {-1.0, 2.0, -3.0, -4.0};
double gt = {-1.0, 2.0, 3.0, -4.0};
deconvolution (
Win, Vin, N, J, L, ht, gt, Vout);
// Should Print Vout = {40.0, -16.0, -42.0, 24.0, -74.0, -8.0, -8.0, -46.0, -8.0}
// But I get an error
return 0;
}发布于 2018-03-02 06:27:29
函数原型中有一些整数常量声明为指针。
void deconvolution (double *Win
, double *Vin
, int *N
, int *j
, int *L
, double *ht
, double *gt
, double *Vout)但是,当您调用反卷积时,您不会将地址传递给这些常量。
deconvolution ( Win, Vin, N, J, L, ht, gt, Vout); // &N,&J,&L你应该把反卷积改为正常的整数,因为你不改变函数中的N,J,L,所以没有理由把地址传递给它们。
void deconvolution (double *Win
, double *Vin
, int N
, int j
, int L
, double *ht
, double *gt
, double *Vout)Vout设置为NULL,因此如果您需要写入Vout,您将得到一个错误,您需要分配内存,这些内存可以在函数反卷积内完成,如果您已经知道大小,则可以在外部进行。如果要在函数中分配Vout,则需要将double* Vout更改为double** Vout,以便可以更改Vout指向的内容。
发布于 2018-03-02 06:01:35
正如coderredoc所说,Vout没有初始化为内存,它需要类似于double *Vout = malloc(N*sizeof(double))。
但是,对您来说更有用的是启用编译器警告(例如,在GCC中,它是用-Wall完成的)。在这种情况下还有更多的警告。然后我会修正这些警告。
然后在调试器(例如GDB)中运行程序。调试器向您显示哪一行崩溃,然后查看它崩溃的原因:该行做什么/需要什么。在本例中,行的名称为Vout[t] = ...,因此您对自己说:“让我检查Vout是否为空指针”。
发布于 2018-03-02 06:18:41
这是不恰当的密码。
首先,函数定义不正确。在主函数中,你把反褶积函数称为“反卷积函数(Win,Vin,N,J,L,ht,gt,Vout);但是,N,J,L不是指针变量。
如果你想把这种形式称为“无效反褶积(double *Win,double *Vin,int N,int j,int L,double *ht,double *gt,double *Vout),或者你应该把反褶积称为反卷积(Win,Vin,&N,&J,&L,ht,gt,Vout);
接下来,"double *Vout = NULL;“不是数组。是指针变量。这意味着Vout上没有分配内存地址。如果您想得到适当的结果,应该编写"double Vout9;“。
最后,Win、Vin、ht和gt还没有很好的定义。在代码中,它是数组。但是,在您的代码中,它是双类型变量。所以,你应该写"Win[],Vin[],ht[],gt[]“。
您将更改下面的代码,您可以得到良好的结果。
int (int,const * argv[]) {
int N = 9; // Size of Win and Vin
int J = 3; // Levels
int L = 4; // Size of gt and ht
double Vout[9]; // output will be stored here
double Win[] = {1.0, -2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, -9.0};
double Vin[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, -7.0, 8.0, 9.0};
double ht[] = {-1.0, 2.0, -3.0, -4.0};
double gt[] = {-1.0, 2.0, 3.0, -4.0};
deconvolution (Win, Vin, &N, &J, &L, ht, gt, Vout);
// Should Print Vout = {40.0, -16.0, -42.0, 24.0, -74.0, -8.0, -8.0, -46.0, -8.0}
// But I get an error
return 0;}
https://stackoverflow.com/questions/49063436
复制相似问题