I have written this function for the sine wave that takes different values of frequency(f0)and sampling frequency(fs) Can anyone help where I am doing wrong.

function y= sin_f(length,amplitude,Frequency,phaseShift)
N=length;
f0 =Frequency;
phi= phaseShift;
A=amplitude;
y=zeros(1,N+1);
for n=1:N+1
y(n)=A*sin(2* pi* f0 * (n-1)+ phi);
end
N=40; % Number of samples
A=1.5; % Amplitude
k=1; % for subplot
phi =pi/2; % for phase angle
fs = 44000
x=[0:1:N];
m = [0, 0.05, 0.1,0.4,0.45,0.5,0.55,0.6]
%m=[0 .1 .2 .8 .9 1 1.1 1.2];
r=floor(length(m)/2);
for i= m
f0=m
y= sin_f(N,A,f0,phi);
figure(1)
subplot(r+1,2,k);
stem(x,y);
xlabel('Time Index n');
ylabel('Amplititude');
title(m(k));
k=k+1;
end

Answers (1)

You tried to use length as the name of a parameter and also as the function that it more commonly represents.
Your code has no comments about what sizes are expected for the parameters, and does no checking; your code just crashes for no good reason if the user passes something non-scalar.

7 Comments

I have written this function that uses dofferent values of w (angular frequency) now I am trying to modify it such that it accepts the frequency f0, and the sampling frequency Fs, instead of the angular frequency w
function y= sin_n(length,amplitude,angularFrequency,phaseShift)
N=length;
w =angularFrequency;
phi= phaseShift;
A=amplitude;
y=zeros(1,N+1);
for n=1:N+1
y(n)=A*sin(w*(n-1)+phi);
end
N=40; % Number of samples
A=1.5; % Amplitude
k=1; % for subplot
phi =pi/2; % for phase angle
x=[0:1:N];
m=[0 .1 .2 .8 .9 1 1.1 1.2];
r=floor(length(m)/2);
for i=m
w =i*pi;
y= sin_n(N,A,w,phi);
figure(1)
subplot(r+1,2,k);
stem(x,y);
xlabel('Time Index n');
ylabel('Amplititude');
title(m(k));
k=k+1;
end
As I am a beginner, I need to be guided in detail. I know its oretty simple but I have just started using matlab and there are many things that i need to learn . I would appreciate if someone guide me in easy way. thanks
Please think about this code:
ABC = [1 4 1 4 2 1 3 5];
disp('length(ABC) is')
length(ABC)
disp('ABC([2 3 4]) is')
ABC([2 3 4])
length = [3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3];
disp('length(ABC) is')
length(ABC)
Why did length(ABC) change?
so u mean i should change name of N= length in the function? if its so then why is it working for angular frequency?
N is not the problem. The problem is "length". You should avoid using variable names that are the same as MATLAB functions, because you will tend to forget that they are variables at the time you need the function.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 28 Oct 2017

Commented:

on 29 Oct 2017

Community Treasure Hunt

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

Start Hunting!