0

i have question,about which i am too much interested,suppose that i have two M-file in matlab, in the first one i have described following function for calculating peaks and peaks indeces

function [peaks,peak_indices] = find_peaks(row_vector)
    A = [0 row_vector 0];
    j = 1;
    for i=1:length(A)-2
        temp=A(i:i+2);
        if(max(temp)==temp(2))
            peaks(j) = row_vector(i);
            peak_indices(j) = i;
            j = j+1;
        end
    end
end

and in second M-file i have code for describing sinusoidal model for given data sample

function [ x ]=generate(N,m,A3)

f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))';
wn = rand(length(t),1).*2 - 1;
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;

end

my question is how to combine it together?one solution would be just create two M-file into folder,then call function from one M-file and made operation on given vector and get result,and then call second function from another M file on given result and finally get what we want,but i would like to build it in one big M-file,in c++,in java,we can create classes,but i am not sure if we can do same in matlab too,please help me to clarify everything and use find_peaks function into generate function

UPDATED: ok now i would like to show simple change what i have made in my code

function [ x ] = generate(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = rand(length(t),1).*2 - 1;
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
[pks,locs] = findpeaks(x);
end

i used findpeaks built-in function in matlab,but i am getting following error

generate(1000,50,50)
Undefined function 'generate' for input arguments of type 'double'.

also i am interested what would be effective sampling rate to avoid alliasing?

1
  • If you aim to write object-oriented codes within MATLAB (as in java and c++), you can easily achieve that! Commented Mar 21, 2013 at 13:37

2 Answers 2

4

You can simply put both in one file. The file must have the same name as the first function therein, and you will not be able to access subsequently defined functions from outside that file. See the MATLAB documentation on functions http://www.mathworks.co.uk/help/matlab/ref/function.html (particularly the examples section).

Also note that MATLAB has a built-in function findpeaks().

(By the way, you're still sampling at too low a frequency and will most certainly get aliasing - see http://en.wikipedia.org/wiki/Aliasing#Sampling_sinusoidal_functions )

Edit: As you requested it, here is some more information on the sampling theorem. A good and simple introduction to these basics is http://www.dspguide.com/ch3/2.htm and for further reading you should search for the Shannon/Nyquist sampling theorem.

Sign up to request clarification or add additional context in comments.

2 Comments

so what would be relevant sampling rate?
also error is this Undefined function 'generate' for input arguments of type 'double'.
2

Try with this, within a single MATLAB script

function test()
clc, clear all, close all
x = generate(1000,50,50);
[p,i] = find_peaks(x)
end

function x = generate(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = rand(length(t),1).*2 - 1;
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
end

function [peaks,peak_indices] = find_peaks(row_vector)
    A = [0;row_vector;0];
    j = 1;
    for i=1:length(A)-2
        temp=A(i:i+2);
        if(max(temp)==temp(2))
            peaks(j) = row_vector(i);
            peak_indices(j) = i;
            j = j+1;
        end
    end
end

8 Comments

but when i run test,it shows only some values not,indexes for exmaple
I edited the answer. btw I would suggest to work around your peak function, you could avoid for loops.
use function findpeaks from matlab?how can i use it?
@fpe It should be noted that the term "script" in MATLAB regards an m-file that cannot accept input and doesn't produce output values, and as such does not contain the function keyword. On the other hand, what you implemented is a function test that also contains local functions generate and find_peaks.
@EitanT: you're right, sometimes I over-use the term script. Btw, I guess that the OP is asking for something similar to what I did, didn't he?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.