2015-02-09 75 views
1

我想在Matlab中生成一个连续的脉冲。我想在5khz生成信号,持续时间为0.01s,然后0.09s没有任何信号,然后重新开始。这是一种矩形脉冲,除了它在5khz。如何以0.01s的持续时间和0.09s的延迟在5khz产生脉冲?

我有下面的代码输出为0.01s 5kHz时的波形,

function [ output ] = FreqGen() 
%UNTITLED3 Summary of this function goes here 
% Detailed explanation goes here 
fs = 44100; 
T = 0.01; 
t = 0:(1/fs):T; 
f = 5000; 
a = 0.5; 
output = a*sin(2*pi*f*t); 
end 

,但我没能弄清楚如何使用Matlab的功能pulsetran产生0.09s脉冲。

就像下面的情节:

enter image description here

+1

这没有意义; 5kHz波形的周期是0.2ms,即0.0002秒。这与你引用的数字相符合的程度如何? – 2015-02-09 21:55:23

+0

我可能是错的。 MOre的一般问题是如何使用pulsetran生成具有另一个自定义波函数的波形? – Foreverniu 2015-02-09 22:01:17

+0

根据您的图表,您希望生成频率为10 Hz或0.1 s的波形。 – rayryeng 2015-02-09 22:10:31

回答

1
pulstran

现在。

关于pulstran的简短文档。语法是:

y = pulstran(t, d, function_handle, p1, p2, ..) 

t是用于计算脉冲(也总时间和输出的尺寸),d是脉冲中心(移位增量)和p1, p2, ..是附加参数的函数的时间步长。

所以输出结果类似于d所有元素的总和function(t+d(i), p1, p2, ..)

下面是代码:

function Untitled() 

[t, y] = FreqGen(5e2, 20e3, 1, 1, 0.01, 0.1); 
figure; 
plot(t(1:3e3), y(1:3e3)); 
xlabel('time [s]'); 

end 

function [t, y] = FreqGen(f, fs, T, A, Pulseduration, Interpulseduration) 
% f - frequency of sine wave [Hz], fs - sampling frequency [Hz], T - total 
% duration [s], A - amplitude of sine wave, Pulseduration [s], 
% Interpulseduration [s] 

% time steps 
t = 0 : (1/fs) : T; 

% pulse center steps 
d = 0 : Interpulseduration : T; 

% use pulstrans 
y = pulstran(t, d, @my_pulse, A, f, Pulseduration); 

end 

function y = my_pulse(t, A, f, Tend) 
% Sine pulse from 0 to Tend 
    y = zeros(size(t)); 
    tx = t > 0 & t < Tend; 
    y(tx) = A * sin(2 * pi * f * t(tx)); 
end 

这虽然比previous answer慢一点。

+0

Trilarion,非常感谢您提供两个很好的答案。对此,我真的非常感激! – Foreverniu 2015-02-13 15:14:28

2

pulstran的文档是不是真的有帮助。虽然可以直接查看该功能,但实际上它是实现自己想要的最简单的方法(并且避开了信号处理工具箱)。在这里,我做到了:

function Untitled() 

[t, y] = FreqGen(5e2, 20e3, 1, 1, [0.01, 0.09]); 
figure; 
plot(t(1:3e3), y(1:3e3)); 
xlabel('time [s]'); 

end 

function [t, y] = FreqGen(f, fs, T, A, Tr) 
% f - frequency of sine wave [Hz], fs - sampling frequency [Hz], T - total 
% duration [s], A - amplitude of sine wave, Tr - duration of high/low state 
% of rectangular envelope pattern [s] 

% time steps 
t = 0 : (1/fs) : T; 

% first the sine wave 
ys = A * sin(2 * pi * f * t); 

% then the rectangular envelope 
yr = double(mod(t, sum(Tr)) < Tr(1)); 

% multiply both 
y = ys .* yr; 

end 

的矩形包络计算取模和比较的帮助。

它看起来像:

enter image description here

+0

非常感谢,这正是我所期待的。 – Foreverniu 2015-02-11 02:14:05