f



Simulink 'Moving average' equivalent code in Matlab

Dear All,

       I have written a 'Moving average' code in embedded matlab function block but I am trying to do the same but with a dynamic size of buffer.

The Required logic of moving average is: (This is not a code but it is just a logic of matlab code should look like)

n (Length of buffer) = round(fs/f0); 
fs = 1KHz; fo must not be > than fs/10
Then n = round(1000/90) =11

n2 > n but n2 is a power of 2.
Array x[n2],y[n2]
Clear complete array x.

X[0]=1;   // unit pulse

For (i=0,i < n;i++) 
    y[i]=BufferSum(x[i]);  // unit pulse response
end

What is BufferSum(x)? 
New_BufferSum = old_BufferSum - old_sample + new_sample.


'Moving average' code in embedded matlab function:
function y = fcn(Moving_u)
persistent iCnt
persistent p 
persistent sum
% persistent buffer_length
% Initialize persistent variables
if isempty(iCnt)
    iCnt=1;
end
if isempty(sum)
    sum=0;
end
if isempty(p)
    p=zeros(1,11);
end

if iCnt>11
    iCnt=1;
end

sum = sum-p(iCnt)+Moving_u;
p(iCnt) = Moving_u;
iCnt=iCnt+1;

y=sum;


Any suggestions!!
0
8/3/2012 8:15:06 AM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

0 Replies
725 Views

Similar Articles

[PageSpeed] 18

Reply:

Similar Artilces:

'Matlab Code' to 'Embedded Matlab fun code'
Dear friends, I have a Matlab code to process a pure analog signal 'u'. Now I want to implement it in Simulink and I want to use it in 'Embedded Matlab fun block' so i need to transform 'Matlab code' to 'Embedded Matlab fun code' to perform the task. The Matlab code is as follows: %%%%%%%%%%%%%%%%%%%%% u_abs = abs(u); major_th = 1.5e-1; minor_th = 1.8e-2; major_peak_value = []; minor_peak_value = []; pos = 1; pos_max = 1; while ~isempty(pos) [m pos_max(end+1)] = max( u_abs( pos : pos + 10) ); pos...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal'''''''''''''' #2
Hi, Thanks for ur help there HJ. I know how to do the tasks you specified there. I would like for the update query to use field values from some of the fields on the form (frmInvoices) such as InvoiceNumber, DateFrom, DateTo. My problem is that an append/update query can't find the values in the open Form (frmInvoices) when I specify them as; [Forms]![frmInvoices]![InvoiceNumber] a select query has no problem finding the field values on a form. please help. Aaron Hi Aaron, Could you post the entire code that you are having trouble with? Now it is not possible to see what goes wrong. HJ "Aaron" <aaron@rapid-motion.co.uk> wrote in message news:260d7f40.0408120245.2f3d01f8@posting.google.com... > Hi, > > Thanks for ur help there HJ. > > I know how to do the tasks you specified there. > > I would like for the update query to use field values from some of the > fields on the form (frmInvoices) such as InvoiceNumber, DateFrom, > DateTo. My problem is that an append/update query can't find the > values in the open Form (frmInvoices) when I specify them as; > > [Forms]![frmInvoices]![InvoiceNumber] > > a select query has no problem finding the field values on a form. > > please help. > > Aaron First off, if you are not always using all the parameters specified in your form, then you have to add parameters to your query on the fly. Also, you can't just do something like qdf.SQL = "SE...

Simulink's 'MATLAB Fcn' vector output problem
Hi! I have met with the following problem: When I put in my model the Simulink's MATLAB Fcn block, which output should be a vector of reals, I get the following error: "Evaluation of expression resulted in an invalid output. Only finite double vector or matrix outputs are supported." I am specifying the corrects option in "Output dimensions:" - 3 in my case, "Output signal type:" - "real". This problem appeared at Matlab 6.5 (R13) under UNIX, before that everything worked fine in Matlab 6.1 (R12.1) under Win.. Has anyone met ...

MATLAB GUI: Is it possible to run '' generate code'' from a figure automatically in a .m file(without clicking the option)?
Hi all, I'm working on a GUI which can generate figures from simulation results. I would like to know if there is a function or command that runs ''generate code'' (from a figure) automatically without clicking the option in menu bar? Because I want to get all the user changes that the user has made in the figure, e.g. the user may drag the legend to another position. And among all the features or commands of MATLAB, I think ''generate code'' may be the only one that helps(or do you have any suggestions?) But it's quite troublesome if I click g...

Matlab equivalent of 'where' in IDL
Hi, Matlab newbie, i.e. me, is trying to set up a loop to go through a data file where if a condition is met (i.e., if 'altitude' is within a range), compute the average of those data points and save to a file. I am having trouble with the conditional statement, as I do not want to create an intermediate file with all the data of interest before being able to compute that average. In IDL, one would write something like: x = where(altitude ge x and altitude le y) avg = mean(x) What is the Matlab equivalent of 'where' in IDL? or what is the way to do this? Any suggestion ...

Performance of Matlab 'zip' and 'unzip' functions
I'm using matlab 6.5 and I just realized that it comes with zip and unzip functions. On the first try it seems that the matlab zip functions are slower than other commonly available programs e.g. winzip, winrar. Anyone has any reliable data about the performance of Matlab zip and unzip functions? Failing that, personal experience is good too. Thanks! Terence Leong wrote: > > > I'm using matlab 6.5 and I just realized that it comes with zip and > unzip functions. On the first try it seems that the matlab zip > functions are slower than other commonly available programs ...

Simulink block for function 'any' in matlab
hi all is there 'any' function block in simulink like the one in matlab tak for din sko !! I think you have to make one yourself. Just an idea: -> Insert vector into "~=0" block and insert the outgoing vector into a Sum block (with one port only). The output is zero if all elements in the vector are zero and 1 otherwise. i tried that it gives the end value which is incorrect any other !! thanks a lot anyway "A " <dragonboy.ali@gmail.com> wrote in message <fcrdan$kmf$1@fred.mathworks.com>... > > i tried that > > it g...

matlab equivalent of 'system' in scilab
hey searchin for matlab equivalent of "system" in scilab On 27 mai, 08:17, Geetika <geetika.ge...@gmail.com> wrote: > hey > searchin for matlab equivalent of "system" in scilab Geetika, You should crosspost this kind of query on the Matlab newsgroup. ;-) All scilabers are not former matlabers Samuel Le 27/05/2010 08:17, Geetika a �crit : > hey > searchin for matlab equivalent of "system" in scilab Please look at the host function, you also may look to unix unix_s unix_g (despite of their names these functions aloso work on Windows and Mac) ...

converting 'C' codes into matlab
How to convert programme written in C language into matlab codes.Please give any example program to convert c code into Matlab code. by jeya "jeya lakshmi" <jeya_vedha@yahoo.co.in> wrote in message <gjcs3l$knu$1@fred.mathworks.com>... > How to convert programme written in C language into matlab codes.Please give any example program to convert c code into Matlab code. > > by > jeya first it is 'c', not 'C'. 'c' code: double myfunct(double a) { return a*2; } matlab code: function b=myfunct(a) b=a*2; end On Dec 30, 4:04=A0am, &...

matlab syntax for 'for workspace' in simulink
Hello everyone, Can I please get help with this, I have searched the internet but could not find any information related to what exactly is the syntax for giving a value to the variable of 'for workspace' in simulink. I am using this syntax in my m-file: sim('decoding','simin',mat); but it's giving the following error: ??? Error using ==> code1 at 6 block_diagram does not have a parameter named 'simin'. how exactly do i give a parameter to simulink model from the workspace? Your question isn't clear. Are you trying to set the name of a variable into a From Workspace block, or are you trying to set a (model's) input parameters when using the sim function? If the former, then you need to use set_param. Something like the following (with appropriate modification to your specific model) set_param('modelname\blockname','VariableName','newvarname') If the latter, then how to get the allowable model parameters is shown in documentation for sim, but it'll be something like sim('modelname','ExternalInputs','yourData'); Phil. ...

'^=' and '~='?
Hello, What is the difference between '^=' and '~='? Thanks, Duckhye ...

'is not' or '!='
A newbie question to you; what is the difference between statements like: if x is not None: and if x != None: Without any context, which one should be preferred? IMHO, the latter is more readable. On 2014-08-18 21:35, ElChino wrote: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: > > Without any context, which one should be preferred? > IMHO, the latter is more readable. > "x == y" tells you whether x and y refer to objects that are equal. "x is y" tells you whether x and y actually refer to the same object. In the case of singletons like None (there's only one None object), it's better to use "is". "ElChino" <elchino@cnn.cn>: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: Do the following: take two $10 bills. Hold one bill in the left hand, hold the other bill in the right hand. Now, the bill in the left hand "is not" the bill in the right hand. However, the bill in the left hand "==" the bill in the right hand. > Without any context, which one should be preferred? > IMHO, the latter is more readable. In almost all cases, both tests would result in the same behavior. However, the "is not" test is conceptually the correct one since you want...

How to display a 'displacement field' or 'displacement vector' in Matlab?
Hi All, I am doing a non-rigid image registration project, and just came across a question that how to display a 'displacement field' or 'displacement vector' in Matlab? I found some similar question at: http://www.mathworks.com.au/matlabcentral/answers/8030-displacement-vector without a good solution. Basically, we have got two two images (2D or 3D) in same size. And by using the registration we can get a 'displacement field' which will transform the coordinates of one image to another. Examples of the graph of the 'displacement field' is here in 2D:...

calling '!' or 'system' in matlab different than same functions from shell
Hi everyone, When I am in the shell (tcsh) on my Mac (outside Matlab), I am able to use the command line utility ncdump to read netcdf format files (http://www.unidata.ucar.edu/software/netcdf/): ncdump -h testfile.nc However, in Matlab, the same command preceded by the '!' (or using 'system') generates an error: >> !ncdump -h test.nc dyld: Library not loaded: /opt/local/lib/libnetcdf.7.dylib Referenced from: /opt/local/bin/ncdump Reason: Incompatible library version: ncdump requires version 10.0.0 or later, but libnetcdf.7.dylib provides version ...

Matlab draw 'subaxis' based subplots on specified 'axes'
Hi smart guys, I am coding some GUI staff using Matlab. And I want to plot a figure with subfigures in one specified 'axes' using 'subaxis' method (which can be download on Matlab FX [subaxis.m][1]). The program behaves quite right at first. As the subfigures are updating by one click button. Then the error pops-up. I simplify the problem and write some testing codes as following: % Specify an 'axes' in my GUI (here is an example of axes handle called 'ax') ax = axes; axes(ax); cla(ax, 'reset'); % Plot something using &#...

Matlab's 'graycomatrix' function
I have one question: Is Matlab's 'graycomatrix' function actually in accordance with original Haralick's 1973 paper? I mean, are they supposed to be the same algorithm? If yes, I don't think Matlab did a good job on this. For example, try it on this matrix (i.e., 'image'): 0 0 1 1 0 0 1 1 0 2 2 2 2 2 3 3 This matrix is in Haralick's original paper and the results can be compared. Can anybody ellaborate if you used it? Thank you. "Bulent Bayraktar" <bayrakta@purdue.edu> writes: > I have one question: Is Matlab's 'graycomatrix' function actually in > accordance with original Haralick's 1973 paper? > I mean, are they supposed to be the same algorithm? If yes, I don't > think Matlab did a good job on this. > > For example, try it on this matrix (i.e., 'image'): > 0 0 1 1 > 0 0 1 1 > 0 2 2 2 > 2 2 3 3 > > This matrix is in Haralick's original paper and the results can be > compared. > > Can anybody ellaborate if you used it? You do need to understand a bit about the conventions used in the software you are using. For example, the Image Processing Toolbox's default black-to-white range for double-precision inputs is [0, 1]. The reference page for graycomatrix describes in detail what it does. By default, for example, it quantizes the range [0, 1] into 8 levels, resulting in an 8-by-8 co-occurrence matrix. But for your matrix, it is seeing 5 &qu...

MatLab's 'movie' function
Hi! Using MatLab's 'movie' function you can play a movie recorded by the 'getframe' command. It is possible to specify the number of times the movie is played and the frames per second. Unfortunately, I've not found a way to stop the playback of a movie. Is it possible to, let's say I've specified the movie should be played 20 times, to disrupt the playback after an user interrupt?! Thanks a lot, Chris On Sat, 12 Feb 2005 10:40:06 +0100, Christoph ROLZ wrote: >Hi! > >Using MatLab's 'movie' function you can play a movie recorded by the >'getframe' command. It is possible to specify the number of times the >movie is played and the frames per second. > >Unfortunately, I've not found a way to stop the playback of a movie. > >Is it possible to, let's say I've specified the movie should be played >20 times, to disrupt the playback after an user interrupt?! > >Thanks a lot, > Chris if you want to play the movie, tried thi command index is the number of frame in video file. index = 1:100 mov = aviread('filename',index) for i = 1: 100 frame = mov(i).cdata; imshow(frame) end Please try it. ...

Equivalent of Java keyword 'super' in Matlab?
Quick question : When using object aggregation, is it possible to access a parent object's fields from a child object function? ie - is there a keyword or method which can be used in Matlab to obtain the same functionality as the Java keyword 'super'? thanks for any tips, Rich "Richard" <richardpjohnson@wanadoo.fr> wrote in message news:eef821e.-1@webx.raydaftYaTP... > Quick question : > > When using object aggregation, is it possible to access a parent > object's fields from a child object function? > > ie - is there a keyword or method...

'Weighted Moving Average' block in Simulink
I wish to understand how the 'weighted moving average' block in Simulink works... Would someone pls share a SIMPLE example on using 'weighted moving average' block? Warmest regards... ...

Can Matlab write 'single' or 'double' precsion grayscale Tiffs?
Hi If I open a 32-bit or 64-bit grayscale Tiff into MatLab and then write it again it is converted into an 8-bit image. imwrite(Image,'image.tif','Compression','none') but if Image is 32-bit or 64-bit floating point then image.tif gets written as an 8-bit image. How can I maintain the precision when using imwrite to save the image? Thanks John ...

Matlab gets stuck in 'busy' state while running a code.
Hello, I have a function called Fitness which accepts only one row vector of size (1,5) at a time. The values in the vector should be between 1 to 37. It is an optimisation code. e.g x=[ 2 6 31 8 17 ] I created a matrix M of size (N,5) using 'rand'. All values in the matrix M are real and are within 1 to 37. Using the codes below I pass one row at a time to the function Fitness. filename.m -------------------------------------------------------------------------------------------------- fu='fitness'; %Fitness function v=length(M); % Length ...

matlab being 'Killed'
Hi I have a problem with matlab saying just 'Killed' then exiting when large data is used. Is there some parameters one can adjust to prevent this ? What is the cause of the problem ? Memory ? -sgrongst ...

matlab 'Killed'
Hi I have a problem with matlab saying just 'Killed' then exiting when large data is used. Is there some parameters one can adjust to prevent this ? What is the cause of the problem ? Memory ? -sgrongst ...

How can I run my matlab code in Linux 'screen' shell?
I want to run my matlab code in linux 'screen' program. and im not sure how to do that. I initiate the screen, and then write matlab -nodisplay. so the matlab starts. however, im not sure how can i run my code in it. shall i just copy paste my code in the shell? how can i copy text from windows and paste it in screen? (since its a long code and i dont want to write it again) "Mineh" wrote in message <isi6uc$nfg$1@newscl01ah.mathworks.com>... > I want to run my matlab code in linux 'screen' program. and im not sure how to do that. I initiate the screen, and t...

Web resources about - Simulink 'Moving average' equivalent code in Matlab - comp.soft-sys.matlab

Resources last updated: 3/19/2016 8:13:34 PM