1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| clc;clear;close all;
Fs = 10; T = 1/Fs; t = -2:T:5; pulseWidth = 3; riseTime = 0.5; amplitude = 1;
rectPulse = amplitude * rectpuls(t, pulseWidth);
trapezoidPulse = zeros(1, length(t)); for i = 1:length(t) if t(i) >= -pulseWidth/2 && t(i) < -pulseWidth/2 + riseTime trapezoidPulse(i) = (t(i) + pulseWidth/2) / riseTime; elseif t(i) >= -pulseWidth/2 + riseTime && t(i) < pulseWidth/2 - riseTime trapezoidPulse(i) = amplitude; elseif t(i) >= pulseWidth/2 - riseTime && t(i) < pulseWidth/2 trapezoidPulse(i) = (-t(i) + pulseWidth/2) / riseTime; else trapezoidPulse(i) = 0; end end
figure('Color', 'white', 'Position', [100, 100, 800, 400]) hold on
plot(t, rectPulse, 'b', 'LineWidth', 2, 'DisplayName', '矩形波')
plot(t, trapezoidPulse, 'r--', 'LineWidth', 2, 'DisplayName', '梯形波')
grid on title('脉冲波形对比(矩形波 vs 梯形波)') xlabel('时间 (s)') ylabel('幅度') xlim([-2 5]) ylim([-0.1 1.2]) legend('show', 'Location', 'northeast')
text(2, 0.7, sprintf('脉冲宽度 = %.1fs', pulseWidth), 'FontSize', 10) text(2, 0.6, sprintf('上升时间 = %.1fs', riseTime), 'FontSize', 10) text(2, 0.5, sprintf('采样率 = %dHz', Fs), 'FontSize', 10)
set(gca, 'FontSize', 10)
nfft = 100000; figure(100) subplot(211) plot(((1:1:nfft)-ceil(nfft/2))/nfft*Fs, fftshift(abs(fft(rectPulse, nfft)))) subplot(212) plot(((1:nfft)-ceil(nfft/2))/nfft*Fs, fftshift(abs(fft(trapezoidPulse, nfft))))
|