MATLAB - DFS discrete Fourier series

Question:
It is known that the pulse width of a periodic rectangular sequence accounts for 1/4 of the entire period, and the sampling points of a period are 16 points. Use the Fourier series to calculate the amplitude and phase spectrum of the signal; find the graph of the inverse transformation of the Fourier series, and compare it with the original signal graph.
Problem Analysis:
After sampling, the original signal becomes a discrete signal, and the time-domain period causes the frequency-domain to be discrete. So it corresponds to the discrete Fourier series.
Formula:
DFS
X [ k ] = ∑ n = 0 N − 1 x [ n ] e − j 2 π N kn X[k]=\sum_{n=0}^{N-1} x[n]e^ {-j\frac{2\pi}{N}kn}X[k]=n=0N1x[n]ejN2 p.mkn
IDFS
x [ n ] = 1 N ∑ k = 0 N − 1 X [ k ] e j 2 π N k n x[n]=\frac{1}{N}\sum_{k=0}^{N-1} X[k]e^{j\frac{2\pi}{N}kn} x[n]=N1k=0N1X[k]ejN2 p.mkn
MATLAB only has the function fft() to calculate the fast Fourier transform. So here we need to create function files by ourselves to perform DFS and IDFS operations.
Function file:

function Xk= dfs(xn,N )%定义函数,返回一个变量Xk
n=[0:1:N-1];%采样点
k=n;
WN=exp(-2*pi*j/N);
Xk=xn*(WN.^(n'*k));
end

Note: Pay attention to the difference between dot multiplication and dot square and multiplication and power in any operation that uses matrix. One is matrix operation and the other is algebraic operation.

This MATLAB code implements the discrete Fourier series (Discrete Fourier Series, DFS) based on matrix calculation, and the discrete time series x [ n ] x [n]x [ n ] is converted to a sequence X [ k ] X[k]in the frequency domainX[k]

Among them, the input parameters xn xnx n represents a discrete time series,NNN represents the length of the sequence. The output of the functionxk xkx k represents the corresponding discrete Fourier series coefficient.

In the code, first two row vectors nn are generatedn andkkk , denote the time point in the time domain and the frequency in the frequency domain, respectively. Next, calculate the WN factor needed in the discrete Fourier transform, and the matrixnk nknk , which is anN × NN\times NN×A matrix of N , where theiirow i , jjElements of column j nki , j nk_{i,j}nki,jDefinition ⋅ kj n_i \cdot k_jnikjvalue.

Next, the DFS matrix WN nk WNnk is calculatedW N nk , which is also aN × NN\times NN×A matrix of N , where theiirow i , jjElements of column j WN nki , j WNnk_{i,j}W N n ki,j 表示 W N n i ⋅ k j WN^{n_i\cdot k_j} WNnikjvalue. Finally, with the discrete time series x [ n ] x[n]x [ n ] and DFS matrixWN nk WNnkMultiply W N nk to get the discrete Fourier series coefficientX [ k ] X[k]X [ k ] .
Similarly, IDFS function files can also be created.

function xn = idfs( Xk,N)
n=[0:1:N-1];%采样点
k=n;
WN=exp(-2*pi*j/N);
xn=(Xk*(WN.^(-(n'*k))))/N;
end

script file:

N=16;%规定采样点数
n=0:N-1;
xn=[ones(1,N/4),zeros(1,3*N/4)];
subplot(2,2,1); stem(n,xn); title('x(n)');
Xk=dfs(xn,N);
subplot(2,2,2); stem(n,abs(Xk));  title('|X(k)|');
subplot(2,2,3); stem(n,angle(Xk));  title('arg|X(k)|');
xn1=idfs(Xk,N);
subplot(2,2,4); stem(n,xn1);  title('比较');

Running results:
insert image description here
Topic 2:
The principal value of a known signal sequence is x ( n ) = [ 0 , 1 , 2 , 3 , 2 , 1 , 0 ] x(n)=[0,1,2,3,2 ,1,0]x(n)=[0,1,2,3,2,1,0 ] , showing two cycles of the signal sequence waveform.
Use the Fourier series to calculate the amplitude and phase spectrum of the signal; find the graph of the inverse transformation of the Fourier series, and compare it with the original signal graph.
The program is basically the same as above. What should be noted here is that the value of N is N=length(xn).
code:

x=[0,1,2,3,2,1,0];
x1=x'.*ones(1,2);
xn=x1(:)';
n1=0:13;
subplot(221);
stem(n1,xn);
title('时域原序列')

N=length(xn);
n=0:N-1;
Xk=dfs(xn,N);
subplot(222);
stem(n,abs(Xk));
title('幅度谱');
subplot(223);
stem(n,angle(Xk));
title('相位谱');

xn1=idfs(Xk,N);
subplot(224);
stem(n,xn1);
title('反变换序列比较');

Running results:
insert image description here
Topic 3:
The influence of the number of cycle repetitions on the frequency spectrum of the sequence
It is known that the pulse width of a rectangular sequence accounts for 1/2 of the entire cycle, and the number of sampling points in one cycle is 10. Use Fourier series transformation to find the number of repetition cycles of the signal respectively Amplitude spectrum for 1, 4, 7, 10.
Full code and its explanation:

xn=[ones(1,5),zeros(1,5)]; % 定义一个信号xn,它由5150组成
Nx=length(xn); % 获取xn的长度,Nx=10
Nw=1000;dw=2*pi/Nw; % 定义DFT点数和频率间隔
k=floor((-Nw/2+0.5):(Nw/2+0.5)); % 定义频域上的k值,使k中心在0
for r=0:3; % 对于4个不同的r值(0,1,2,3),生成4组不同的信号和它们的DFT
    K=3*r+1; % 定义K值为3r+1,它控制了信号中的周期性
    nx=0:(K*Nx-1); % 定义时间轴上的n值
    x=xn(mod(nx,Nx)+1); % 生成一个周期性信号,其周期为xn,长度为K*Nx
    Xk=x*(exp(-j*dw*nx'*k))/K; % 计算信号x的DFT,k表示频率轴上的频率值
    subplot(4,2,2*r+1); stem(nx,x) % 绘制信号x的时域波形
    axis([0,K*Nx-1,0,1.1]); ylabel('x(n)'); % 设置绘图范围和标签
    subplot(4,2,2*r+2); plot(k*dw,abs(Xk)) % 绘制信号x的频域幅度谱
    axis([-4,4,0,1.1*max(abs(Xk))]); ylabel('X(k)'); % 设置绘图范围和标签
end

Key explanation:
(1) k=floor((-Nw/2+0.5):(Nw/2+0.5));
This code is to define an array of k values ​​in the frequency domain with a length of Nw, so that the center of the k array The corresponding frequency is 0, that is, the center of k is 0. The specific explanation is as follows:
Nw is the number of DFT points, which means that there are Nw points on the discrete frequency axis. Usually, Nw is a power of 2, such as Nw=256, 512, etc.
dw is the interval between two adjacent frequency points on the frequency axis, and its value can be calculated by 2π/Nw.
(-Nw/2+0.5): (Nw/2+0.5) is an array of length Nw+1, which contains all integers from -Nw/2+0.5 to Nw/2+0.5. Among them, 0.5 is to make the frequency corresponding to the center of the k array be 0, that is, to shift the k-axis coordinate from the integer value by 0.5.
floor is a rounding down function, which is used to round down the floating-point numbers in the k array to the nearest integer. This is to ensure that all values ​​in the k array are integers, which is convenient for subsequent calculation and drawing. The center frequency is set to 0 for the convenience of representing the symmetry of the signal in the frequency domain, that is, the real part and the imaginary part are respectively symmetrical. In addition, because DFT is a discrete transformation, the number of points on the frequency axis is limited, so we need to represent the frequency components of the signal as accurately as possible from the limited sampling points to reduce the sampling error as much as possible. Therefore, the sampling points on the frequency axis need to be distributed as densely as possible. However, in DFT, the number of points on the frequency axis is limited, and it is usually a power of 2, so perfect sampling cannot be achieved, that is, the frequency axis cannot be divided densely enough, resulting in discretization errors. Therefore, in order to minimize the sampling error, an offset of 0.5 is usually added to the frequency axis so that the center of the sampling point falls on an integer point, which can minimize the discretization error. In addition, adding an offset of 0.5 has another advantage, that is, the sampling points on the frequency axis can be mapped to the sampling points on the time domain, so that the calculation process and results of DFT can be understood more intuitively. Therefore, the function of k=floor((-Nw/2+0.5):(Nw/2+0.5)) is to define a sequence of integers from -Nw/2 to Nw/2, where the frequency corresponding to the center of the sequence is 0 , in order to calculate the frequency components of the DFT.

(2) x=xn(mod(nx,Nx)+1);
The function of this line of code is to generate a periodic signal with a period of xn and a length of KNx, where K is a constant and nx is a signal from 0 A sequence of integers to KNx-1.
Specifically, the function of mod(nx,Nx) is to take all the elements in nx modulo Nx, so that the result is an integer sequence of length K Nx, each Nx element is a group , and the elements in the group are the same . For example, if Nx=5, K=3, then the result of mod(nx,Nx) is [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4].
Because xn is a vector with a length of Nx, the integer sequence obtained by mod(nx,Nx) can be used as an index of xn, that is, mod(nx,Nx)+1 means that the corresponding value is taken from xn. Because vectors in MATLAB are 1-based, you need to add 1 to the index.
In this way, x=xn(mod(nx,Nx)+1) obtains a
periodic signal of length K Nx, whose period is xn, which is obtained by repeating xn continuously K times. This construction is often used to analyze the properties of periodic signals.
In short, the role of mod is to limit the value of nx to within Nx, so as to realize the recycling of xn vectors.
Running results:
insert image description here
Result analysis:
The more cycles of the signal sequence, the more the spectrum is concentrated to several frequency points. When the number of signal cycles approaches infinity, the spectrum is transformed into a discrete spectrum.

Guess you like

Origin blog.csdn.net/m0_46155417/article/details/129385147