|
路线栈欢迎您!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
三维数据可视化为学习者提供直观影像,应用广泛。3D折线图旨在展示三维空间的曲线分布,采用MATLAB函数plot3。与二维折线绘制plot函数的差异在于,需要设定x,y,z方向坐标向量,其他绘图属性两者基本一致。与之类似的还有3D散点图scatter3,3D杆状图stem3等。
下面,我们以傅里叶级数展开(Fourier series expansion)3D可视化作为代码示例。
- clc; clear; close all;
- %----------------------
- m = 15; % Series
- N = 512; % Period
- T0 = 4;
- T = 2;
- t1 = linspace(-T/2, T/2, N);
- t2 = linspace(T/2, T0-T/2, N);
- t = [(t1-T0)'; (t2-T0)'; t1'; t2'; (t1+T0)'];
- %----------------------
- % Construct periodic signals
- %----------------------
- % s = zeros(5*N,1);
- % s(1:N)=1; s(2*N+1:3*N)=1; s(4*N+1:5*N)=1;
- s = [ones(N,1);zeros(N,1);ones(N,1);zeros(N,1);ones(N,1)]';
- y = zeros(m+1, length(s));
- y(m+1,:) = s-0.5;
- plot3(t-(t+2), t, y(m+1,:),'LineWidth',2); % Period square wave
- hold on;
- axis([-2,m+1,-T0-1,T0+1,-1,2])
- set(gca,'XTick',-T0-1:2:m);
- set(gca,'YTick',-T0-1:1:T0+1);
- set(gca,'ZTick',-1:0.5:T0);
- title('Fourier series expansion','FontSize',15);
- grid on;
- view(-49,23)
- fsamp = 1028;
- f = linspace(1, m+1, fsamp);
- A = 0.5; % Coefficient
- freq = 1:1:m;
- plot3(f,T0+f-f+1,A*sinc(A*f)*5,'LineWidth',3)
- mag = A*sinc(A*freq)*5;
- h=stem3(freq,T0+freq-freq+1,mag,'filled','LineWidth',3);
- set(h,'Marker','o',...
- 'MarkerFaceColor','g')
- x = A*ones(size(t));% Harmonic signals
- X = A*ones(size(t));% Synthesis signals
- for k = 1:m
- x = 2*A*sinc(A*k)*cos(2*pi*t*k/T0);
- X = X+2*A*sinc(A*k)*cos(2*pi*t*k/T0);
- y(k, :) = x;
- plot3(k+t-t, t, y(k,:),'LineWidth',1.5); % Harmonic
- end
- plot3(k+1+t-t, t, X-0.5,'LineWidth',2);
- grid on;
- xlabel('Frequency view','Rotation', 15);
- ylabel('Time duration','Rotation', -10);
- zlabel('Amplitude/Magnitude');
- set(gcf,'Color','White'); % Background is white
- %----------------------
复制代码
程序运行如图-1所示。
图-1 傅里叶级数展开的三维可视化
|
|