C语言编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容 拷贝至 3 个其他数组中(在 main()中声明这 4 个数组)。

编写一个程序,初始化一个 double 类型的数组,然后把该数组的内容 拷贝至 3 个其他数组中(在 main()中声明这 4 个数组)。使用带数组表示法的 函数进行第 1 份拷贝。使用带指针表示法和指针递增的函数进行第 2 份拷贝。 把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。第 3 个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指 针。也就是说,给定以下声明,则函数调用如下所示:

double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);

#include <stdio.h>
#include <Windows.h>

void copy_arr(double target1[], double s1[], int x) {
	int i;

	for (i = 0; i < x; i++) {
		target1[i] = s1[i];
	}
	return;
}

void copy_ptr(double target2[], double s2[], int y) {
	double *t_index;
	double *s_index;
	int j;

	t_index = target2;
	s_index = s2;
	for (j = 0; j < y; j++) {
		*(t_index + j) = *(s_index + j);
	}

	return;
}

void copy_ptrs(double target3[], double s3[], double * index) {

	int i = 0;

	while (s3 < index) {
		target3[i] = *s3;
		s3++;
		i++;
	}

	return;
}

int main(void) {
	double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
	double target1[5];
	double target2[5];
	double target3[5];

	void copy_arr(double target1[], double s1[], int x);
	void copy_ptr(double target2[], double s2[], int y);
	void copy_ptrs(double target3[], double s3[], double * index);

	copy_arr(target1, source, 5);
	copy_ptr(target2, source, 5);
	copy_ptrs(target3, source, source + 5);
	printf("target 1 :   \n");
	printf("%.1f %.1f %.1f %.1f\n", target1[0], target1[1], target1[2], target1[3]);
	printf("target 2 :   \n");
	printf("%.1f %.1f %.1f %.1f\n", target2[0], target2[1], target2[2], target2[3]);
	printf("target 3 :   \n");
	printf("%.1f %.1f %.1f %.1f\n", target3[0], target3[1], target3[2], target3[3]);

	system("pause");
	return 0;
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
由于傅里叶变换涉及到复杂的数学计算,因此在C语言需要使用一些库来实现。下面是一个示例程序,使用fftw库实现傅里叶变换,并使用gnuplot库画出频谱图像: ```c #include <stdio.h> #include <stdlib.h> #include <fftw3.h> #include <gnuplot_c.h> #define SAMPLE_RATE 44100 #define WINDOW_SIZE 1024 int main(int argc, char** argv) { if (argc != 2) { printf("Usage: %s audio_file.bin\n", argv[0]); return 1; } // 打开音频文件 FILE* fp = fopen(argv[1], "rb"); if (fp == NULL) { printf("Error: cannot open file %s\n", argv[1]); return 1; } // 读取音频数据 short* data = (short*)malloc(WINDOW_SIZE * sizeof(short)); if (data == NULL) { printf("Error: memory allocation failed\n"); return 1; } // 初始化FFT变换 fftw_complex* in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * WINDOW_SIZE); fftw_complex* out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * WINDOW_SIZE); fftw_plan plan = fftw_plan_dft_1d(WINDOW_SIZE, in, out, FFTW_FORWARD, FFTW_ESTIMATE); // 初始化gnuplot gnuplot_ctrl* h = gnuplot_init(); gnuplot_setstyle(h, "lines"); gnuplot_set_xlabel(h, "Frequency (Hz)"); gnuplot_set_ylabel(h, "Magnitude"); // 读取音频数据并进行FFT变换 int num_samples = 0; while (!feof(fp)) { num_samples = fread(data, sizeof(short), WINDOW_SIZE, fp); if (num_samples == 0) break; // 将音频数据拷贝到FFT输入缓冲区 for (int i = 0; i < WINDOW_SIZE; i++) { in[i][0] = data[i]; in[i][1] = 0; } // 执行FFT变换 fftw_execute(plan); // 计算频谱 double freq_step = (double)SAMPLE_RATE / WINDOW_SIZE; double* freq = (double*)malloc(WINDOW_SIZE * sizeof(double)); double* mag = (double*)malloc(WINDOW_SIZE * sizeof(double)); for (int i = 0; i < WINDOW_SIZE; i++) { freq[i] = i * freq_step; mag[i] = sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]); } // 绘制频谱图像 gnuplot_resetplot(h); gnuplot_plot_xy(h, freq, mag, WINDOW_SIZE, "Spectrum"); // 释放内存 free(freq); free(mag); } // 释放资源 fclose(fp); free(data); fftw_destroy_plan(plan); fftw_free(in); fftw_free(out); gnuplot_close(h); return 0; } ``` 说明: 1. 程序通过命令行参数传入音频文件名。 2. 程序使用fftw库来进行傅里叶变换。fftw库是一个高效的傅里叶变换库,可以自动选择最优的计算方式。 3. 程序使用gnuplot库来绘制频谱图像。gnuplot库是一个通用的绘图库,可以在C语言方便地使用。 4. 程序每次读取WINDOW_SIZE个样本进行FFT变换,然后绘制频谱图像,直到文件结束。 5. 程序使用了一些常量,例如采样率和窗口大小,可以根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止境12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值