Skip to content

Commit

Permalink
fixed converters and the static interface and added an r static examp…
Browse files Browse the repository at this point in the history
…le for Jade
  • Loading branch information
kevinhughes27 committed Jul 31, 2013
1 parent ee75cd5 commit 5c3603f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/undocumented/r_static/graphical/converter_jade_bss.R
@@ -0,0 +1,45 @@
# Blind Source Separation using the Jade Algorithm with Shogun
#
# Based on the example from scikit-learn
# http://scikit-learn.org/
#
# Kevin Hughes 2013

library('sg')

# Generate sample data
n_samples <- 2000
time <- seq(0,10,length=n_samples)

# Source Signals
S <- matrix(0,2,n_samples)
S[1,] <- sin(2*time)
S[2,] <- sign(sin(3*time))
S <- S + 0.2*matrix(runif(2*n_samples),2,n_samples)

# Standardize data
S <- S * (1/apply(S,1,sd))

# Mixing Matrix
A <- rbind(c(1,0.5),c(0.5,1))

# Mix Signals
X <- A %*% S
mixed_signals <- matrix(X,2,n_samples)

# Separating
sg('set_converter', 'jade')
sg('set_features', 'TRAIN', mixed_signals)
S_ <- sg('apply_converter')

# Plot
par(mfcol=c(3,1));

plot(time, S[1,], type="l", col='blue', main="True Sources", ylab="", xlab="")
lines(time, S[2,], type="l", col='green')

plot(time, X[1,], type="l", col='blue', main="Mixed Sources", ylab="", xlab="")
lines(time, X[2,], type="l", col='green')

plot(time, S_[1,], type="l", col='blue', main="Estimated Sources", ylab="", xlab="")
lines(time, S_[2,], type="l", col='green')
7 changes: 7 additions & 0 deletions src/shogun/ui/SGInterface.cpp
Expand Up @@ -5381,6 +5381,9 @@ bool CSGInterface::cmd_set_converter()

bool CSGInterface::cmd_apply_converter()
{
if (m_nrhs!=1 || !create_return_values(1))
return false;

CDenseFeatures<float64_t>* conv_features = ui_converter->apply();
SGMatrix<float64_t> converted_mat = conv_features->get_feature_matrix();
set_matrix(converted_mat.matrix,converted_mat.num_rows,converted_mat.num_cols);
Expand All @@ -5390,6 +5393,10 @@ bool CSGInterface::cmd_apply_converter()
bool CSGInterface::cmd_embed()
{
int32_t target_dim = get_int_from_int_or_str();

if (m_nrhs!=1 || !create_return_values(1))
return false;

CDenseFeatures<float64_t>* embedding = ui_converter->embed(target_dim);
SGMatrix<float64_t> embedding_matrix = embedding->get_feature_matrix();
set_matrix(embedding_matrix.matrix,embedding_matrix.num_cols,embedding_matrix.num_rows);
Expand Down

0 comments on commit 5c3603f

Please sign in to comment.