1. c語言進行特定格式文本文件的篩選與判斷
/*
原始數據:
9300100 10
9300101 10
9300102 10
9300110 10
9300111 10
9301110 20
9301111 20
9301112 20
處理後數據:
930010 10
930011 10
930111 20
Press any key to continue
*/
#include<stdio.h>
#defineN60
intmain(){
inti,j,n,k,flag,data1[N],data2[N];
FILE*fp=fopen("indata.txt","rt");
if(fp==NULL){
printf("無法打開數據文件。 ");
return1;
}
printf("原始數據: ");
for(n=0;!feof(fp)&&n<N;++n){
fscanf(fp,"%d,%d",&data1[n],&data2[n]);
printf("%d %d ",data1[n],data2[n]);
data1[n]/=10;//去除末位
}
fclose(fp);
for(i=0;i<n-1;++i){
flag=0;
for(j=i+1;j<n;++j){
if(data1[j]==data1[i]&&data2[j]==data2[i]){
for(k=j;k<n-1;++k){
data1[k]=data1[k+1];
data2[k]=data2[k+1];
}
--n;
flag=1;
}
}
if(flag)--i;
}
printf("處理後數據: ");
for(i=0;i<n;++i)
printf("%d %d ",data1[i],data2[i]);
return0;
}
2. c語言命令行在一個文件中查找另一個文件指定的內容
f=fopen("filter.txt","r");
f1=fopen("serieA.txt","r");
while(fgets(line,50,f)!=NULL)
{
while(fgets(line,50,f1)!=NULL)
if(strcmp(nameF,nameS)==0).....
rewind(f1);
}
3. C語言實現fir1函數
#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#endif
#define SAMPLE double /* define the type used for data samples */
void clear(int ntaps, SAMPLE z[])
{
int ii;
for (ii = 0; ii < ntaps; ii++) {
z[ii] = 0;
}
}
SAMPLE fir_basic(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[])
{
int ii;
SAMPLE accum;
/* store input at the beginning of the delay line */
z[0] = input;
/* calc FIR */
accum = 0;
for (ii = 0; ii < ntaps; ii++) {
accum += h[ii] * z[ii];
}
/* shift delay line */
for (ii = ntaps - 2; ii >= 0; ii--) {
z[ii + 1] = z[ii];
}
return accum;
}
SAMPLE fir_circular(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
int *p_state)
{
int ii, state;
SAMPLE accum;
state = *p_state; /* the filter's state to a local */
/* store input at the beginning of the delay line */
z[state] = input;
if (++state >= ntaps) { /* incr state and check for wrap */
state = 0;
}
/* calc FIR and shift data */
accum = 0;
for (ii = ntaps - 1; ii >= 0; ii--) {
accum += h[ii] * z[state];
if (++state >= ntaps) { /* incr state and check for wrap */
state = 0;
}
}
*p_state = state; /* return new state to caller */
return accum;
}
SAMPLE fir_shuffle(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[])
{
int ii;
SAMPLE accum;
/* store input at the beginning of the delay line */
z[0] = input;
/* calc FIR and shift data */
accum = h[ntaps - 1] * z[ntaps - 1];
for (ii = ntaps - 2; ii >= 0; ii--) {
accum += h[ii] * z[ii];
z[ii + 1] = z[ii];
}
return accum;
}
SAMPLE fir_split(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
int *p_state)
{
int ii, end_ntaps, state = *p_state;
SAMPLE accum;
SAMPLE const *p_h;
SAMPLE *p_z;
/* setup the filter */
accum = 0;
p_h = h;
/* calculate the end part */
p_z = z + state;
*p_z = input;
end_ntaps = ntaps - state;
for (ii = 0; ii < end_ntaps; ii++) {
accum += *p_h++ * *p_z++;
}
/* calculate the beginning part */
p_z = z;
for (ii = 0; ii < state; ii++) {
accum += *p_h++ * *p_z++;
}
/* decrement the state, wrapping if below zero */
if (--state < 0) {
state += ntaps;
}
*p_state = state; /* return new state to caller */
return accum;
}
SAMPLE fir_double_z(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
int *p_state)
{
SAMPLE accum;
int ii, state = *p_state;
SAMPLE const *p_h, *p_z;
/* store input at the beginning of the delay line as well as ntaps more */
z[state] = z[state + ntaps] = input;
/* calculate the filter */
p_h = h;
p_z = z + state;
accum = 0;
for (ii = 0; ii < ntaps; ii++) {
accum += *p_h++ * *p_z++;
}
/* decrement state, wrapping if below zero */
if (--state < 0) {
state += ntaps;
}
*p_state = state; /* return new state to caller */
return accum;
}
SAMPLE fir_double_h(SAMPLE input, int ntaps, const SAMPLE h[], SAMPLE z[],
int *p_state)
{
SAMPLE accum;
int ii, state = *p_state;
SAMPLE const *p_h, *p_z;
/* store input at the beginning of the delay line */
z[state] = input;
/* calculate the filter */
p_h = h + ntaps - state;
p_z = z;
accum = 0;
for (ii = 0; ii < ntaps; ii++) {
accum += *p_h++ * *p_z++;
}
/* decrement state, wrapping if below zero */
if (--state < 0) {
state += ntaps;
}
*p_state = state; /* return new state to caller */
return accum;
}
int main(void)
{
#define NTAPS 6
static const SAMPLE h[NTAPS] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
static SAMPLE h2[2 * NTAPS];
static SAMPLE z[2 * NTAPS];
#define IMP_SIZE (3 * NTAPS)
static SAMPLE imp[IMP_SIZE];
SAMPLE output;
int ii, state;
/* make impulse input signal */
clear(IMP_SIZE, imp);
imp[5] = 1.0;
/* create a SAMPLEd h */
for (ii = 0; ii < NTAPS; ii++) {
h2[ii] = h2[ii + NTAPS] = h[ii];
}
/* test FIR algorithms */
printf("Testing fir_basic:\n ");
clear(NTAPS, z);
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_basic(imp[ii], NTAPS, h, z);
printf("%3.1lf ", (double) output);
}
printf("\n\n");
printf("Testing fir_shuffle:\n ");
clear(NTAPS, z);
state = 0;
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_shuffle(imp[ii], NTAPS, h, z);
printf("%3.1lf ", (double) output);
}
printf("\n\n");
printf("Testing fir_circular:\n ");
clear(NTAPS, z);
state = 0;
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_circular(imp[ii], NTAPS, h, z, &state);
printf("%3.1lf ", (double) output);
}
printf("\n\n");
printf("Testing fir_split:\n ");
clear(NTAPS, z);
state = 0;
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_split(imp[ii], NTAPS, h, z, &state);
printf("%3.1lf ", (double) output);
}
printf("\n\n");
printf("Testing fir_double_z:\n ");
clear(2 * NTAPS, z);
state = 0;
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_double_z(imp[ii], NTAPS, h, z, &state);
printf("%3.1lf ", (double) output);
}
printf("\n\n");
printf("Testing fir_double_h:\n ");
clear(NTAPS, z);
state = 0;
for (ii = 0; ii < IMP_SIZE; ii++) {
output = fir_double_h(imp[ii], NTAPS, h2, z, &state);
printf("%3.1lf ", (double) output);
}
#ifdef WIN32
printf("\n\nHit any key to continue.");
getch();
#endif
return 0;
}
1. fir_basic: 實現基本的FIR濾波器
2. fir_circular: 說明環行buffer是如何實現FIR的。
3. fir_shuffle: 一些TI的處理器上使用的shuffle down技巧
4. fir_split: 把FIR濾波器展開為兩塊,避免使用環行緩存。
5. fir_double_z: 使用雙精度的延遲線,使可以使用一個flat buffer。
6. fir_double_h: 使用雙精度的系數,使可以使用一個flat buffer。
4. FIR濾波器的C語言程序
length==256
5. 一段matlab低通濾波器程序,求改編成C語言。
這個我剛好做過一個濾波器,事實上對時域信號做FFT,截取一定點數再做逆FFT相當於理想濾波。設計濾波器代碼如下:
f1=100;f2=200;%待濾波正弦信號頻率
fs=2000;%采樣頻率
m=(0.3*f1)/(fs/2);%定義過度帶寬
M=round(8/m);%定義窗函數的長度
N=M-1;%定義濾波器的階數
b=fir1(N,f2/fs);%使用fir1函數設計濾波器
%輸入的參數分別是濾波器的階數和截止頻率
figure(1)
[h,f]=freqz(b,1,512);%濾波器的幅頻特性圖
%[H,W]=freqz(B,A,N)當N是一個整數時函數返回N點的頻率向量和幅頻響應向量
plot(f*fs/(2*pi),20*log10(abs(h)))%參數分別是頻率與幅值
xlabel('頻率/赫茲');ylabel('增益/分貝');title('濾波器的增益響應');
figure(2)
subplot(211)
t=0:1/fs:0.5;%定義時間范圍和步長
s=sin(2*pi*f1*t)+sin(2*pi*f2*t);%濾波前信號
plot(t,s);%濾波前的信號圖像
xlabel('時間/秒');ylabel('幅度');title('信號濾波前時域圖');
subplot(212)
Fs=fft(s,512);%將信號變換到頻域
AFs=abs(Fs);%信號頻域圖的幅值
f=(0:255)*fs/512;%頻率采樣
plot(f,AFs(1:256));%濾波前的信號頻域圖
xlabel('頻率/赫茲');ylabel('幅度');title('信號濾波前頻域圖');
figure(3)
sf=filter(b,1,s);%使用filter函數對信號進行濾波
%參數分別為濾波器系統函數的分子和分母多項式系數向量和待濾波信號輸入
subplot(211)
plot(t,sf)%濾波後的信號圖像
xlabel('時間/秒');ylabel('幅度');title('信號濾波後時域圖');
axis([0.2 0.5 -2 2]);%限定圖像坐標范圍
subplot(212)
Fsf=fft(sf,512);%濾波後的信號頻域圖
AFsf=abs(Fsf);%信號頻域圖的幅值
f=(0:255)*fs/512;%頻率采樣
plot(f,AFsf(1:256))%濾波後的信號頻域圖
xlabel('頻率/赫茲');ylabel('幅度');title('信號濾波後頻域圖');
6. C語言怎麼編寫:請編寫一個字元串過濾程序,若字元串中出現多個相同的字元,將非首次出現的字元過濾掉。
結果出來了,你看看吧,滿意請採納
#include<stdio.h>
#include<string.h>
voidfinddd(charx[]);
intmain()
{
chara[150];
char*aa;
inta1;
inti;
printf("請輸入任意的字元串:");
gets(a);
finddd(a);
return0;
}
voidfinddd(charx[])//不帶數據返回
{
inti,shu=0;
charaa[100];
intaaa[200]={0};
for(i=0;i<strlen(x);i++)
{
if(aaa[x[i]]==0)
{
aaa[x[i]]=1;
aa[shu++]=x[i];
}
}
aa[shu++]='