Create a sinus wave with increasing steps frequencies matlab -
i'm novice in matlab programing , thank in advance help. generate un sinus wave function starting 0.25 hz , increasing every 8 oscillations 0.05 hz until 0.7 hz. amplitude constant.
i try code:
cyc = 8; % number of cycles 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 help.
1) try this:
cyc = 8; % number of cycles wave = []; t = []; 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 comment, code memory preallocation:
cyc = 8; % number of cycles npoints = 100; % number of points in 1 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 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:
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))'; % matlab version less 2016b [t,ind] = unique(t); wave = wave(ind); % remove duplicate values plot(t,wave)
Comments
Post a Comment