How to realize a transfer function with higher order of numerator than denominator in octave? - signal-processing

a=1
% Construct the trasfer function
num=[a 1 3]
den=[1 2 10]
G=tf(num,den)
% Impulse response
impulse(G)
% Step response
step(G)
When I click on 'run' this error appears "error: Order numerator >= order denominator"

If you follow the error on the terminal, it suggests that line 95 in imp_invar.m of the control package is to blame. (if you don't know where this was installed, you can find out by typing pkg list in your terminal)
If you convert this error to a warning, the code continues. Obviously you do so at your own risk. I would make a backup of the original .m file just in case.
Note that the same code run on matlab does not issue any error or warning (which is odd in itself, given the stark note about invalid impulse invariance in this scenario from octave ... there is a reference quoted inside imp_invar.m if you're interested.)

Related

Lua function returns 768 values - how to put these into a table

I am programming an instrument that uses Lua with extensions for accessing the instrument controls and displays.
The instrument has a command to read the analyzer plotted points for frequency and amplitude:
:analyzer:trace:frequency?
and
:analyzer:trace:amplitude?
Whenever either of the above is executed, 768 values are returned (frequency or amplitude). This is an embedded test system running Lua for user apps - I can't change the command.
I tried to use table.pack() to put these returned values into a table, but the syntax of the command with the ":" causes an error.
Code I tried --
freq = {};
freq = table.pack(:analyzer:trace:frequency?);
Error message is -- tests.lua:2428: unexpected symbol near ':'
Whenever the :analyzer:trace:frequency? is run stand alone (from PuTTY) or as a line in the Lua code, there is no error.
PuTTY receives 768 frequency values, each separated by a comma.
Looking for ways to direct the return into an array/table or to wrap the command so that it will execute similarly as above.
Thanks

Can't interpret SPSS Error Message in MATRIX code

A program to run a Schmid-Leiman transformation using SPSS's Matrix language was published in 2005 by Woolf & Preising in Behavior Research Methods volume 37, pages 48 to 58). It is probably not important for you to know what a Schmid-Leiman transformation is, but I'll explain in comments if you feel it is necessary.
In modifying the program for my own data, I'm getting an error I can't figure out:
Error # 12302 in column 12. Text: ,
Syntax error.
Execution of this command stops.
Error in RIGHT HAND SIDE of COMPUTE command.
The MATRIX statement skipped.
Here is the beginning of the code. The error is showing as coming in Line 6:
* Encoding: UTF-8.
* Schmid-Leiman Solution for 2 level higher-order Factor analysis.
Matrix.
* ENTER YOUR SPECIFICATIONS HERE.
* Enter first-order pattern matrix.
Compute F1={.461, .253, -.058, -.069;
.241, .600, .143, .033;
.582, .047, -.077, -.125;
.327, .297, -.120, -.166;
.176, .448, -.240, -.099;
.680, .069, -.036, -.138;
.415, .228, -.091, -.153;
.
.
.
.390, .205, .002, -.098;
.164, .369, -.170, -.047
}.
As shown above, the text generating the error is shown as a comma (,), but the actual text (following the COMPUTE statement) in column 12 is an open bracket ({). So I have no idea what is going on. Can someone help?
For reference, the original code as proposed by Woolf & Preising (2005) is found here;
The Woolf & Preising article is found here
PS: The sample program given in the link above does run on my copy of SPSS. Here's the beginning of that code:
* Schmid-Leiman Solution for 2 level higher-order Factor analysis.
Matrix.
* ENTER YOUR SPECIFICATIONS HERE.
* Enter first-order pattern matrix.
Compute F1={0.099, 0.5647, -0.1521;
0.0124, 0.9419, -0.1535;
-0.1501, 0.6177, 0.4218;
0.7441, -0.0882, 0.1425;
0.6241, 0.2793, -0.1137;
0.8693, -0.0331, 0.0289;
-0.0154, -0.2706, 0.6262;
-0.0914, 0.0995, 0.7216;
0.1502, 0.0835, 0.398}.

Creating a simple Rcpp package with dependency with other Rcpp package

I am trying to improve my loop computation speed by using foreach, but there is a simple Rcpp function I defined inside of this loop. I saved the Rcpp function as mproduct.cpp, and I call out the function simply using
sourceCpp("mproduct.cpp")
and the Rcpp function is a simple one, which is to perform matrix product in C++:
// [[Rcpp::depends(RcppArmadillo, RcppEigen)]]
#include <RcppArmadillo.h>
#include <RcppEigen.h>
// [[Rcpp::export]]
SEXP MP(const Eigen::Map<Eigen::MatrixXd> A, Eigen::Map<Eigen::MatrixXd> B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}
So, the function in the Rcpp file is MP, referring to matrix product. I need to perform the following foreach loop (I have simplified the code for illustration):
foreach(j=1:n, .package='Rcpp',.noexport= c("mproduct.cpp"),.combine=rbind)%dopar%{
n=1000000
A<-matrix(rnorm(n,1000,1000))
B<-matrix(rnorm(n,1000,1000))
S<-MP(A,B)
return(S)
}
Since the size of matrix A and B are large, it is why I want to use foreach to alleviate the computational cost.
However, the above code does not work, since it provides me error message:
task 1 failed - "NULL value passed as symbol address"
The reason I added .noexport= c("mproduct.cpp") is to follow some suggestions from people who solved similar issues (Can't run Rcpp function in foreach - "NULL value passed as symbol address"). But somehow this does not solve my issue.
So I tried to install my Rcpp function as a library. I used the following code:
Rcpp.package.skeleton('mp',cpp_files = "<my working directory>")
but it returns me a warning message:
The following packages are referenced using Rcpp::depends attributes however are not listed in the Depends, Imports or LinkingTo fields of the package DESCRIPTION file: RcppArmadillo, RcppEigen
so when I tried to install my package using
install.packages("<my working directory>",repos = NULL,type='source')
I got the warning message:
Error in untar2(tarfile, files, list, exdir, restore_times) :
incomplete block on file
In R CMD INSTALL
Warning in install.packages :
installation of package ‘C:/Users/Lenovo/Documents/mproduct.cpp’ had non-zero exit status
So can someone help me out how to solve 1) using foreach with Rcpp function MP, or 2) install the Rcpp file as a package?
Thank you all very much.
The first step would be making sure that you are optimizing the right thing. For me, this would not be the case as this simple benchmark shows:
set.seed(42)
n <- 1000
A<-matrix(rnorm(n*n), n, n)
B<-matrix(rnorm(n*n), n, n)
MP <- Rcpp::cppFunction("SEXP MP(const Eigen::Map<Eigen::MatrixXd> A, Eigen::Map<Eigen::MatrixXd> B){
Eigen::MatrixXd C = A * B;
return Rcpp::wrap(C);
}", depends = "RcppEigen")
bench::mark(MP(A, B), A %*% B)[1:5]
#> # A tibble: 2 x 5
#> expression min median `itr/sec` mem_alloc
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt>
#> 1 MP(A, B) 277.8ms 278ms 3.60 7.63MB
#> 2 A %*% B 37.4ms 39ms 22.8 7.63MB
So for me the matrix product via %*% is several times faster than the one via RcppEigen. However, I am using Linux with OpenBLAS for matrix operations while you are on Windows, which often means reference BLAS for matrix operations. It might be that RcppEigen is faster on your system. I am not sure how difficult it is for Windows user to get a faster BLAS implementation (https://csgillespie.github.io/efficientR/set-up.html#blas-and-alternative-r-interpreters might contain some pointers), but I would suggest spending some time on investigating this.
Now if you come to the conclusion that you do need RcppEigen or RcppArmadillo in your code and want to put that code into a package, you can do the following. Instead of Rcpp::Rcpp.package.skeleton() use RcppEigen::RcppEigen.package.skeleton() or RcppArmadillo::RcppArmadillo.package.skeleton() to create a starting point for a package based on RcppEigen or RcppArmadillo, respectively.

F#: Path error when trying to run simple F# program on VS Code

I just installed Visual Studio Code on a different laptop and then proceeded to get the Ionide-fsharp extension. I then tried to run this simple piece of code:
open System
let rec factorial n =
if n = 0
then 1
else n * factorial (n - 1)
factorial 5
But then I get this error and I can't seem to fix it:
open: the term 'open' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling on the name,
of if a path was included, verify that the path is correct and try again.
It then proceeds to throw a bunch more nonsense at me and repeats the error above for every single line of code I have written afterwards. Please help.

a signal x(n) then is this concept of shirting and folding correct?

x(n) is given
need x(-n+3)
so to solve it:
first advance the x(n) signal by 3 units(time)
then fold it, or make a reflection of it
are the above steps correct or is the following correct
first fold the x(n) signal
then advance the signal by 3 units
?
Yes, this is a common source of confusion when learning about signals. Here's what I usually do.
Let y[n] = x[-n+3]. Because of -n, y[n] is obviously a time-reversed version of x[n]. But the question about the shift remains.
Notice that y[3] = x[0]. Therefore, y[n] is achieved by first reflecting x[n] about n=0 and then delaying the reflected signal by 3.
For example, let x[n] be the unit step function u[n]. Draw x[n], then draw y[n].
Actually here is what I do:
Let
x(n) = {1,-1,2,4,-3,0,6,-3,-1,2,7,9,-7,5}
^
Suppose origin or n=0 is 6. Note that the ^ symbol indicates the origin. First, we find the folder sequence of x(-n) from x(n). So first we fold or we can say reverse the form of x(n), we get,
The folder sequence of x(-n) from x(n) is
x(-n) = {5,-7,9,7,2,-1,-3,6,0,-3,4,2,-1,1}
^
then shift the sequence of x(-n) towards right hand side by 3 units, we will get
x(-n+3) = {5,-7,9,7,2-1,-3,6,0,-3,4,2,-1,1}
^
Now, the sample 4 is at the origin.
Above steps are correct.
The following steps can be corrected too if these are followed like:
first fold the x(n) signal
then delay the signal by 3 units this will yield x(-n+3).

Resources