Making a Square Wave Gradually
显示 更早的评论
I made a script where I have a square wave audioplayer made from sine waves based off of
for k = 1:2:order
x = x + (sin(k*2*pi.*f.*t))/k;
y((k+1)/2,:) = x;
pause(.01)
end
where f is an array based off the equation 2^x, and t is another array 0:1/fs:T used for sampling. f is used to select frequencies in an exponential manner so that the pitch goes up linearly. This worked very well, but I wanted to try the same thing but flipped around. The script I'm working on takes a certain frequency and creates a waveform that gradually evolves from a sine wave to a square wave. This is what I have so far:
function GradualSquareWave(order,f,T,fs)
tic
t = 0:1/fs:T;
timeDomain = 0:1:(fs * T);
y = zeros(size(t * order));
x = zeros(size(t));
h = waitbar(0,'Initializing Fourier Series');
for k = 1:2:order
x = x + (sin(k*2*pi*f.*t))/k;
y(1 + timeDomain + (((k+1)/2)-1)*fs) = x;
if k/order > .05
waitbar(k/order,h,sprintf('Computing Fourier Series %6.2g%%',(100 * k)/order))
end
pause(.01)
end
elapsedTime = toc;
if elapsedTime < 1
waitbar(1,h,sprintf('Playing Square Wave to Order %d after %6.4g seconds',order,elapsedTime))
end
if elapsedTime >= 1
waitbar(1,h,sprintf('Playing Square Wave to Order %d after%6.4g seconds',order,elapsedTime))
end
player = audioplayer(y,fs);
play(player)
pause(T)
close(h)
It seems that most of it is working just fine and dandy, but I'm not getting the full transition. For each k value, the audioplayer plays a one-second section of the wave form and cuts off at T seconds. If there aren't T sections to play, the last section is extended to T. I have no idea why each section is played for precisely one second. I want each section to last T/((order+1)/2) seconds so that each section of the wave form is played equally throughout time T. If anybody could help me out, I'd really appreciate it.
1 个评论
Walter Roberson
2014-5-22
As a debugging step, examine size(y) before the "for k" loop, and also after the "for k" loop. Also give us some sample input parameters to test with.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Texas Instruments C2000 Processors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!