常见窗函数的幅度谱、相位谱(DTFT)
作者:互联网
昨天手机弹窗新闻,日本前首相安倍街头演讲时遇刺,“百年未有之大变局”……
继续做题。
一、 在数字信号处理(DSP)中非常有用的四种有限长序列,也称为窗函数。将各自幅度谱归一化(即幅度最大值为1),然后画出图形。
二、上代码
1 // Book Author:Vinay K Ingle, John G Proakis 2 // 3 // Problem 3.4 4 // script by: KY 5 // 6 clear, clc, clf(); 7 8 mode(2); 9 funcprot(0); 10 load('../fun/lib'); 11 12 // ----------------------------------------------------------------------------- 13 // START Output Info about this sce-file 14 mprintf('\n***********************************************************\n'); 15 [ban1,ban2] = fun_banner(); 16 mprintf("\n <DSP using MATLAB> 3rd edition, Problem 3.4 \n"); 17 mprintf(" ----------------------------------------------------------\n\n"); 18 19 // ----------------END---------------------------------------------------------- 20 21 22 //% ---------------------------------------------- 23 //% Rectangle Window sequence 24 //% ---------------------------------------------- 25 M = 101; 26 27 n1_start = 0; n1_end = M; 28 n1 = [n1_start : n1_end - 1]; 29 30 x1 = ones(1, length(n1)); 31 32 // ----------------------------------------------------------------------------- 33 // --------------------START f0 figure----------------------------------------- 34 f0=scf(0); //creates figure with id==0 and make it the current one 35 f0.figure_size=[1000,700]; // adjust window size 36 f0.background=8; // add background color 8-white 37 f0.figure_name="Problem 3.4 Rectangle"; // name window 38 subplot(1, 1, 1); 39 40 plot(n1,x1,'bo'); 41 plot(n1,x1,'b.'); 42 plot2d3(n1,x1,2); // Create plot with blue line 43 title("$x1(n)\ Rectangle\ sequence$",'fontname',7,'fontsize',4); 44 //title("mprintf('x1(n) sequence, M = %d', M)"); 45 xlabel('$n$','fontname',3); ylabel('x1(n)','fontname',3,'fontsize',3); 46 xgrid(5,0,7); // color, thickness, style 47 a0=get("current_axes"); // get the handle of the newly created axes 48 //a0.data_bounds=[-4,6,-6,6]; 49 // ----------------------------------------------------------------------------- 50 // --------------------END f0 ------------------------------------------------- 51 52 53 MM = 500; 54 k = [-MM:MM]; // [-pi, pi] 55 //k = [0:M]; // [0, pi] 56 w = (%pi/MM) * k; 57 58 [X1] = fun_dtft(x1, n1, w); 59 60 magX1 = abs(X1); angX1 = fun_angle(X1); realX1 = real(X1); imagX1 = imag(X1); 61 62 //%% --------------------------------------------------------------------------- 63 //%% START X(w)'s mag ang real imag 64 // --------------------START f1 figure----------------------------------------- 65 f1=scf(1); //creates figure with id==0 and make it the current one 66 f1.figure_size=[1000,700]; // adjust window size 67 f1.background=8; // add background color 8-white 68 f1.figure_name="Problem 3.4 DTFT of Rm(n)"; // name window 69 subplot(2, 1, 1); 70 plot(w/%pi,magX1/max(magX1)); 71 title("$Magnitude\ Response$",'fontname',7,'fontsize',4); 72 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('Magnitude |X|','fontname',3,'fontsize',3); 73 xgrid(5,0,7); // color, thickness, style 74 a0=get("current_axes"); // get the handle of the newly created axes 75 //a0.data_bounds=[-4,6,-6,6]; 76 77 subplot(2, 1, 2); 78 plot(w/%pi,angX1/%pi*exp(0)); 79 title("$Phase\ Response$",'fontname',7,'fontsize',4); 80 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('$Radians/\pi*exp(0)$','fontname',3,'fontsize',3); 81 xgrid(5,1,7); // color, thickness, style 82 a1=get("current_axes"); // get the handle of the newly created axes 83 //a1.data_bounds=[-4,6,-2,2]; 84 // ----------------------------END f1------------------------------------------- 85 //%% END X(w)'s mag ang real imag 86 //%% --------------------------------------------------------------------------- 87 88 89 //% -------------------------------------------------- 90 //% Hanning Window Sequence 91 //% -------------------------------------------------- 92 //n2_start = -9; n2_end = 15; 93 //n2 = [n2_start : n2_end]; 94 n2 = n1; 95 x2 = 0.5 * (1 - cos(2*%pi*n1/(M-1))) .* x1; 96 97 // ----------------------------------------------------------------------------- 98 // --------------------START f2 figure----------------------------------------- 99 f2=scf(2); //creates figure with id==0 and make it the current one 100 f2.figure_size=[1000,700]; // adjust window size 101 f2.background=8; // add background color 8-white 102 f2.figure_name="Problem 3.4 x2(n) Hanning"; // name window 103 subplot(1, 1, 1); 104 105 plot(n2,x2,'bo'); 106 plot(n2,x2,'b.'); 107 plot2d3(n2,x2,2); // Create plot with blue line 108 title("$x2(n)\ Hanning\ sequence$",'fontname',7,'fontsize',4); 109 //title(mprintf('x1(n) sequence, M = %d', M)); 110 xlabel('$n$','fontname',3); ylabel('x2(n)','fontname',3,'fontsize',3); 111 xgrid(5,0,7); // color, thickness, style 112 a0=get("current_axes"); // get the handle of the newly created axes 113 //a0.data_bounds=[-4,6,-6,6]; 114 // ----------------------------------------------------------------------------- 115 // --------------------END f2 ------------------------------------------------- 116 117 118 MM = 500; 119 k = [-MM:MM]; // [-pi, pi] 120 //k = [0:M]; // [0, pi] 121 w = (%pi/MM) * k; 122 123 [X2] = fun_dtft(x2, n2, w); 124 125 magX2 = abs(X2); angX2 = fun_angle(X2); realX2 = real(X2); imagX2 = imag(X2); 126 127 //%% --------------------------------------------------------------------------- 128 //%% START X(w)'s mag ang real imag 129 // --------------------START f3 figure----------------------------------------- 130 f3=scf(3); //creates figure with id==0 and make it the current one 131 f3.figure_size=[1000,700]; // adjust window size 132 f3.background=8; // add background color 8-white 133 f3.figure_name="Problem 3.4 DTFT of Cm(n)"; // name window 134 subplot(2, 1, 1); 135 plot(w/%pi,magX2/max(magX2)); 136 title("$Magnitude\ Response$",'fontname',7,'fontsize',4); 137 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('Magnitude |X|','fontname',3,'fontsize',3); 138 xgrid(5,0,7); // color, thickness, style 139 a0=get("current_axes"); // get the handle of the newly created axes 140 //a0.data_bounds=[-4,6,-6,6]; 141 142 subplot(2, 1, 2); 143 plot(w/%pi,angX2/%pi*exp(0)); 144 title("$Phase\ Response$",'fontname',7,'fontsize',4); 145 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('$Radians/\pi*exp(0)$','fontname',3,'fontsize',3); 146 xgrid(5,1,7); // color, thickness, style 147 a1=get("current_axes"); // get the handle of the newly created axes 148 //a1.data_bounds=[-4,6,-2,2]; 149 // ----------------------------END f3------------------------------------------- 150 //%% END X(w)'s mag ang real imag 151 //%% --------------------------------------------------------------------------- 152 153 154 //% -------------------------------------------------- 155 //% Triangular Window Sequence 156 //% -------------------------------------------------- 157 //n3_start = -3; n3_end = 10; 158 //n3 = [n3_start : n3_end]; 159 n3 = n1; 160 x3 = (1 - abs(M-1-2*n3)/(M-1)) .* x1; 161 162 // ----------------------------------------------------------------------------- 163 // --------------------START f4 figure----------------------------------------- 164 f4=scf(4); //creates figure with id==0 and make it the current one 165 f4.figure_size=[1000,700]; // adjust window size 166 f4.background=8; // add background color 8-white 167 f4.figure_name="Problem 3.4 x3(n) Triangular"; // name window 168 subplot(1, 1, 1); 169 170 plot(n3,x3,'bo'); 171 plot(n3,x3,'b.'); 172 plot2d3(n3,x3,2); // Create plot with blue line 173 title("$x3(n)\ Triangular\ sequence$",'fontname',7,'fontsize',4); 174 //title(mprintf('x3(n) sequence, M = %d', M)); 175 xlabel('$n$','fontname',3); ylabel('x2(n)','fontname',3,'fontsize',3); 176 xgrid(5,0,7); // color, thickness, style 177 a0=get("current_axes"); // get the handle of the newly created axes 178 //a0.data_bounds=[-4,6,-6,6]; 179 // ----------------------------------------------------------------------------- 180 // --------------------END f4 ------------------------------------------------- 181 182 MM = 500; 183 k = [-MM:MM]; // [-pi, pi] 184 //k = [0:M]; // [0, pi] 185 w = (%pi/MM) * k; 186 187 [X3] = fun_dtft(x3, n3, w); 188 189 magX3 = abs(X3); angX3 = fun_angle(X3); realX3= real(X3); imagX3 = imag(X3); 190 191 //%% --------------------------------------------------------------------------- 192 //%% START X(w)'s mag ang real imag 193 // --------------------START f5 figure----------------------------------------- 194 f5=scf(5); //creates figure with id==0 and make it the current one 195 f5.figure_size=[1000,700]; // adjust window size 196 f5.background=8; // add background color 8-white 197 f5.figure_name="Problem 3.4 DTFT of Tm(n)"; // name window 198 subplot(2, 1, 1); 199 plot(w/%pi,magX3/max(magX3)); 200 title("$Magnitude\ Response$",'fontname',7,'fontsize',4); 201 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('Magnitude |X|','fontname',3,'fontsize',3); 202 xgrid(5,0,7); // color, thickness, style 203 a0=get("current_axes"); // get the handle of the newly created axes 204 //a0.data_bounds=[-4,6,-6,6]; 205 206 subplot(2, 1, 2); 207 plot(w/%pi,angX3/%pi*exp(0)); 208 title("$Phase\ Response$",'fontname',7,'fontsize',4); 209 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('$Radians/\pi*exp(0)$','fontname',3,'fontsize',3); 210 xgrid(5,1,7); // color, thickness, style 211 a1=get("current_axes"); // get the handle of the newly created axes 212 //a1.data_bounds=[-4,6,-2,2]; 213 // ----------------------------END f5------------------------------------------- 214 //%% END X(w)'s mag ang real imag 215 //%% --------------------------------------------------------------------------- 216 217 218 //% --------------------------------------------- 219 //% Hamming Window sequence 220 //% --------------------------------------------- 221 //n4_start = 0; n4_end = 50; 222 //n4 = [n4_start : n4_end]; 223 n4 = n1; 224 x4 = (0.54 - 0.46 * cos(2 * %pi * n4/(M-1))) .* x1; 225 226 // ----------------------------------------------------------------------------- 227 // --------------------START f6 figure----------------------------------------- 228 f6=scf(6); //creates figure with id==0 and make it the current one 229 f6.figure_size=[1000,700]; // adjust window size 230 f6.background=8; // add background color 8-white 231 f6.figure_name="Problem 3.4 x4(n) Hamming"; // name window 232 subplot(1, 1, 1); 233 234 plot(n4,x4,'bo'); 235 plot(n4,x4,'b.'); 236 plot2d3(n4,x4,2); // Create plot with blue line 237 title("$x4(n)\ Hamming\ sequence$",'fontname',7,'fontsize',4); 238 //title(mprintf('x4(n) sequence, M = %d', M)); 239 xlabel('$n$','fontname',3); ylabel('x2(n)','fontname',3,'fontsize',3); 240 xgrid(5,0,7); // color, thickness, style 241 a0=get("current_axes"); // get the handle of the newly created axes 242 //a0.data_bounds=[-4,6,-6,6]; 243 // ----------------------------------------------------------------------------- 244 // --------------------END f6 ------------------------------------------------- 245 246 247 MM = 500; 248 k = [-MM:MM]; // [-pi, pi] 249 //k = [0:M]; // [0, pi] 250 w = (%pi/MM) * k; 251 252 [X4] = fun_dtft(x4, n4, w); 253 254 magX4 = abs(X4); angX4 = fun_angle(X4); realX4= real(X4); imagX4 = imag(X4); 255 256 //%% --------------------------------------------------------------------------- 257 //%% START X(w)'s mag ang real imag 258 // --------------------START f7 figure----------------------------------------- 259 f7=scf(7); //creates figure with id==0 and make it the current one 260 f7.figure_size=[1000,700]; // adjust window size 261 f7.background=8; // add background color 8-white 262 f7.figure_name="Problem 3.4 DTFT of Hm(n)"; // name window 263 subplot(2, 1, 1); 264 plot(w/%pi,magX4/max(magX4)); 265 title("$Magnitude\ Response$",'fontname',7,'fontsize',4); 266 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('Magnitude |X|','fontname',3,'fontsize',3); 267 xgrid(5,0,7); // color, thickness, style 268 a0=get("current_axes"); // get the handle of the newly created axes 269 //a0.data_bounds=[-4,6,-6,6]; 270 271 subplot(2, 1, 2); 272 plot(w/%pi,angX4/%pi*exp(0)); 273 title("$Phase\ Response$",'fontname',7,'fontsize',4); 274 xlabel('$frequency\ in\ \pi\ units$','fontname',3); ylabel('$Radians/\pi*exp(0)$','fontname',3,'fontsize',3); 275 xgrid(5,1,7); // color, thickness, style 276 a1=get("current_axes"); // get the handle of the newly created axes 277 //a1.data_bounds=[-4,6,-2,2]; 278 // ----------------------------END f7------------------------------------------- 279 //%% END X(w)'s mag ang real imag 280 //%% ---------------------------------------------------------------------------View Code
三、运行结果
0、窗函数时长都是M=101,区间范围取-π到π。
1、矩形窗
2、汉宁窗
3、三角窗
4、汉明窗
标签:fontname,函数,figure,相位,axes,fontsize,get,DTFT,pi 来源: https://www.cnblogs.com/ky027wh-sx/p/16460158.html