Sine generation with variable frequency and amplitude

11 次查看(过去 30 天)
Hi everyone,
I have been trying to write a code that generates series of sine wave with changing frequency and amplitude; for example a 20 Hz sine wave for 20 seconds, after it reaches to 30 Hz for 10 seconds and so on.
When with constant ouput values, there is no problem. The code I wrote for it is below
k=zeros(1,100);
t=zeros(1,100);
t=0:99;
for i=1:100;
if i<=30;
k(i)=1;
else if i<=60;
k(i)=3;
else i<=100;
k(i)=2;
end
end
end
plot(t,k)
It gives correct results. Try this one and imagine I want to change these constant values with sine waves.
When I changed k(i) values with sine, like below;
k=zeros(1,100);
t=zeros(1,100);
t=0:99;
a=linspace(0,2*pi,30);
for i=1:90;
if i<=30;
k(i)=2*sin(a);
else if i<=60;
k(i)=sin(a);
else i<=90;
k(i)=3*sin(a);
end
end
end
plot(t,k)
It gives that error;
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> Untitled3 at 9 k(i)=2*sin(a);
The dimensions match, Can anyone give me a hand about the code above or show any other way?
Thanks,
Ozgur Palaz

采纳的回答

Ozgur
Ozgur 2011-7-8
I think i solve the problem. Here is the code if you are interested;
num=length(t);
k=zeros(1,num);
f1=0.8;
f2=1;
f3=0.5;
fs=100;
T=1/fs;
for i=1:num;
if i<=num/5;
k(i)=2*sin(2*pi*f1*t(i));
else if i<=2*num/5;
k(i)=0;
else if i<=3*num/5;
k(i)=sin(2*pi*f2*t(i));
else if i<=4*num/5;
k(i)=0;
else i<=num;
k(i)=3*sin(2*pi*f3*t(i));
end
end
end
end
end
plot(t,k)
  1 个评论
Chaithra Kc
Chaithra Kc 2020-1-23
I have tried this the code is working but, I'm not able to generate it in the scope. The waveform is as shown in the picture...please help

请先登录,再进行评论。

更多回答(3 个)

Friedrich
Friedrich 2011-7-8
Hi,
You get this error because sin(a) is a vector of size 1x30 and you want assign this to a single field k(i). Maybe give k an other size
k=zeros(100,30)
and than do
k(i,:)=2*sin(a)
  3 个评论
Friedrich
Friedrich 2011-7-8
Sorry my fault this would be correct:
k=zeros(1,100);
t=zeros(1,100);
t=0:99;
a=linspace(0,2*pi,30);
for i=1:90;
if i<=30;
k(i)=2*sin(a(i));
else if i<=60;
k(i)=sin(a(i-30));
else i<=90;
k(i)=3*sin(a(i-60));
end
end
end
but the code from andrei is much better.
Ozgur
Ozgur 2011-7-8
hanks, it results like Andrei suggested. Now I'm tryin on setting the sine frequency in the form of Sin(2*pi*f*t)
Time vector t should have been put into the for loop for this I guess.
Do you have any suggestion about that? Check this link to get what i am trying to manage.
<http://imageshack.us/photo/my-images/20/signalq.jpg/>
Thanks again.
Ozgur

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2011-7-8
k= zeros(1,100);
k(1:90) = sin(linspace(0,2*pi,30))'*[2 1 3];
EDIT
k= zeros(30,100);
k(:,1:90) = reshape(permute(repmat(sin(linspace(0,2*pi,30))'*[2 1 3],[1,1,30]),[1 3 2]),30,[]);
answer on comment
1. '*[2 1 3]
eg.:
>> a = (1:3)'
a =
1
2
3
>> b = [2 1 3]
b =
2 1 3
>> a*b
ans =
2 1 3
4 2 6
6 3 9
or
>> bsxfun(@times,a,b)
ans =
2 1 3
4 2 6
6 3 9
2. Please specify question
  2 个评论
Ozgur
Ozgur 2011-7-8
Thank you for your reply, this creates a sine wave and the error is gone, can you explain more detailed about this '*[2 1 3] expression?
Do you have any idea about how can I set the frequencies of the sine waves, can i do it with "t" and a "f" variable that are put into the format of sine SIN(wt)?
Thanks for your help...
Ozgur
Ozgur 2011-7-8
.
.
.
.
.
Your edit resulted different as Friedrich did before; like below;
http://imageshack.us/photo/my-images/20/signalq.jpg/
The signal i want to get is alike below figure please check it.
http://imageshack.us/photo/my-images/20/signalq.jpg/
Now, the question is about the frequencies. Let me say the first freq of the sine is 10 hz, second and the third is 50 and 20 Hz respectively.
how can i get the values into the code?
As you know, sinus is in the format of SIN(2*pi*f*t) where f is freq in Hz and t is time in sec. Should be there a sampling freq? I'm trying hard on this and i couldn't manage.
Ozgur

请先登录,再进行评论。


Ozgur
Ozgur 2011-7-8
The sine waves i want to realize is alike below figure, I made it on Microsoft paint, so it is a little rough..
Ozgur

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by