简体   繁体   English

创建具有增加的步进频率的正弦波Matlab

[英]Create a sinusoidal wave with increasing steps frequencies matlab

I'm novice in matlab programing and I thank you in advance for your help. 我是matlab编程的新手,在此先感谢您的帮助。 I would like to generate un sinus wave function starting to 0.25 Hz and increasing every 8 oscillations by 0.05 Hz until 0.7 Hz. 我想生成一个非正弦波函数,起始频率为0.25 Hz,每8次振荡增加0.05 Hz,直到0.7 Hz。 The amplitude is constant. 幅度是恒定的。

I try this code: 我尝试以下代码:

cyc = 8; % number of cycles
for lF= 0.25:0.05:0.7 %% Frequency Increment
    t=1/fE:1/fE:(cyc/lF); % time per freq step
    wave = sin(2*pi*t)
end
plot(wave)

Thank you for your help. 谢谢您的帮助。

1) Try this: 1)试试这个:

cyc = 8; % number of cycles
wave = [];
T = [];
for f = 0.25:0.05:0.7 %% Frequency Increment
    t=0.01/f:0.01/f:(cyc/f); % time per freq step
    wave = [wave,sin(2*pi*f*t)];
    if isempty(T)
       T = t;
    else
       T = [T,T(end)+t];
    end
end
plot(T,wave)

2) Following to comment, the code with memory preallocation: 2)在下面的注释中,具有内存预分配的代码:

cyc = 8; % number of cycles
Npoints = 100; % number of points in one cycle
f = 0.25:0.05:0.7; %% Frequency Increment
Nf = length(f);

wave = zeros((cyc*Npoints-1)*Nf+1,1);
T    = zeros((cyc*Npoints-1)*Nf+1,1);
t0   = 0;
t_f  = linspace(0,cyc,cyc*Npoints); % time*frequency per freq step
for nf = 1:length(f)
    ind = (nf-1)*(cyc*Npoints-1)+(1:cyc*Npoints);
    wave(ind) = sin(2*pi*f(nf)*t_f);
    T(ind)    = t0 + t/f(nf);
    t0 = t0+cyc/f(nf);
end
plot(T,wave)

3) Vectorized version : 3) 矢量化版本

t    = cyc*linspace(0,1,cyc*Npoints)';
wave = sin(2*pi*t(:,ones(Nf,1)));
t = bsxfun(@plus,cumsum([0,cyc./f(1:end-1)]),bsxfun(@times,1./f,t))'; % for MATLAB version less than 2016b
[t,ind] = unique(t); wave = wave(ind); % remove duplicate values
plot(t,wave)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM