@@ -47,18 +47,31 @@ int main()
4747 double BER ;//BER代表比特误码率
4848 double progress ;//progress是用来显示进度的变量
4949
50+ // --- 定义csv文件指针 ---
51+ FILE * fp = NULL ;
52+ // 打开hard_viterbi.csv
53+ fp = fopen ("hard_viterbi.csv" , "w" );
54+ if (fp == NULL ) {
55+ printf ("Error: Could not open file for writing.\n" );
56+ return 1 ;
57+ }
58+ // 写入表头
59+ fprintf (fp , "SNR,BER\n" );
60+ // -------------------------
61+
5062 //generate state table 生成状态转移表
5163 statetable ();
5264
5365 //random seed
5466 srand ((int )time (0 ));
5567
5668 //input the SNR and frame number
57- printf ("\nEnter start SNR: " );
69+ printf ("\n--- Hard Decision Viterbi Simulation ---\n" );
70+ printf ("Enter start SNR: " );
5871 scanf ("%f" , & start );
59- printf ("\nEnter finish SNR: " );
72+ printf ("Enter finish SNR: " );
6073 scanf ("%f" , & finish );
61- printf ("\nPlease input the number of message: " );
74+ printf ("Please input the number of message: " );
6275 scanf ("%ld" , & seq_num );
6376
6477 for (SNR = start ; SNR <= finish ; SNR ++ )//外层循环,遍历不同的信噪比
@@ -116,17 +129,30 @@ int main()
116129
117130 //print the intermediate result
118131 printf ("Progress=%2.1f, SNR=%2.1f, Bit Errors=%ld, BER=%E\r" , progress , SNR , bit_error , BER );
132+
133+
134+
119135 }
120136
121137 //calculate the final BER
122138 BER = (double )bit_error / (double )(message_length * seq_num );
123139
140+ // --- 将结果写入文件 ---
141+ // 格式: SNR, BER
142+ fprintf (fp , "%f,%E\n" , SNR , BER );
143+ // 同时在屏幕打印结果
144+ printf ("SNR=%2.1f, Bit Errors=%ld, BER=%E\n" , SNR , bit_error , BER );
145+
124146 //print the final result
125- printf ("Progress=%2.1f, SNR=%2.1f, Bit Errors=%ld, BER=%E\n" , progress , SNR , bit_error , BER );
147+ // printf("Progress=%2.1f, SNR=%2.1f, Bit Errors=%ld, BER=%E\n", progress, SNR, bit_error, BER);
126148 }
127149
128150 //system("pause");
129151
152+ // --- 关闭csv文件 ---
153+ fclose (fp );
154+ printf ("Result saved to hard_viterbi.csv\n" );
155+
130156 return 0 ;
131157}
132158
@@ -296,7 +322,7 @@ void decoder()
296322 for (int curr_state = 0 ; curr_state < 4 ; curr_state ++ )
297323 {
298324 // 如果当前状态不可达,就不用算了
299- if (path_metric [curr_state ] = = INF ) continue ;
325+ if (path_metric [curr_state ] > = INF ) continue ;
300326
301327 // 遍历所有可能的输入位 (0 或 1)
302328 for (input = 0 ; input <= 1 ; input ++ )
0 commit comments