synaptic modifications

This commit is contained in:
Dave Griffiths 2015-08-04 10:14:06 +01:00
parent 7dd525f3c1
commit 4aad99828c
6 changed files with 112 additions and 33 deletions

View File

@ -32,6 +32,11 @@ public:
void process();
void load_source(const std::string &filename);
void load_target(const std::string &filename);
void save_source(const std::string &filename);
void save_target(const std::string &filename);
private:
OSC_server m_osc;
u32 m_source_block_size;

View File

@ -25,6 +25,9 @@
using namespace std;
using namespace spiralcore;
static const u32 NUM_FIXED_SYNAPSES = 1000;
static const double usage_factor = 1000000;
brain::brain() :
m_current_block_index(0),
m_average_error(0),
@ -89,8 +92,6 @@ const block &brain::get_block(u32 index) const {
return m_blocks[index];
}
static const double usage_factor = 1000000;
// returns index to block
u32 brain::search(const block &target, const search_params &params) {
double closest = FLT_MAX;
@ -129,6 +130,7 @@ u32 brain::rev_search(const block &target, const search_params &params) {
return furthest_index;
}
// really slow - every to every comparison of blocks calculating average distance
double brain::calc_average_diff(search_params &params) {
double diff=0;
for (vector<block>::const_iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
@ -140,7 +142,7 @@ double brain::calc_average_diff(search_params &params) {
return diff;
}
void brain::build_synapses(search_params &params, double thresh) {
void brain::build_synapses_thresh(search_params &params, double thresh) {
m_average_error = calc_average_diff(params)*thresh;
double err=m_average_error*thresh;
u32 brain_size = m_blocks.size();
@ -150,6 +152,7 @@ void brain::build_synapses(search_params &params, double thresh) {
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
for (vector<block>::const_iterator j=m_blocks.begin(); j!=m_blocks.end(); ++j) {
if (index!=outer_index) {
// collect connections that are under threshold in closeness
double diff = i->compare(*j,params);
if (diff<err) {
i->get_synapse().push_back(index);
@ -161,6 +164,46 @@ void brain::build_synapses(search_params &params, double thresh) {
}
}
void brain::build_synapses_fixed(search_params &params) {
//m_average_error = calc_average_diff(params)*thresh;
u32 brain_size = m_blocks.size();
u32 outer_index=0;
for (vector<block>::iterator i=m_blocks.begin(); i!=m_blocks.end(); ++i) {
u32 index = 0;
vector<pair<int,double>> collect;
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
// collect comparisons to all other blocks
for (vector<block>::const_iterator j=m_blocks.begin(); j!=m_blocks.end(); ++j) {
if (index!=outer_index) {
double diff = i->compare(*j,params);
collect.push_back(pair<int,double>(index,diff));
}
++index;
}
// sort them by closeness
sort(collect.begin(),collect.end(),
[](const pair<int,double> &a,
const pair<int,double> &b) -> bool {
return a.second<b.second;
});
// add the closest ones to the list
for(u32 n=0; n<NUM_FIXED_SYNAPSES; ++n) {
i->get_synapse().push_back(collect[n].first);
}
++outer_index;
}
}
void brain::jiggle() {
m_current_block_index=rand()%m_blocks.size();
}
u32 brain::search_synapses(const block &target, search_params &params) {
const block &current = get_block(m_current_block_index);
double closest = DBL_MAX;
@ -168,14 +211,20 @@ u32 brain::search_synapses(const block &target, search_params &params) {
// find nearest in synaptic connections
// cerr<<"searching "<<current.get_synapse_const().size()<<" connections"<<endl;
for (vector<u32>::const_iterator i=current.get_synapse_const().begin();
i!=current.get_synapse_const().end(); ++i) {
vector<u32>::const_iterator i=current.get_synapse_const().begin();
u32 synapse_count=0;
// use m_num_synapses to restrict search
// only makes sense when ordered by closeness in fixed mode
while (i!=current.get_synapse_const().end() &&
synapse_count<params.m_num_synapses) {
const block &other = get_block(*i);
double diff = target.compare(other,params);
if (diff<closest) {
closest=diff;
closest_index = *i;
}
++i;
++synapse_count;
}
deplete_usage();

View File

@ -35,6 +35,9 @@ public:
// rewrites whole brain
void init(u32 block_size, u32 overlap, window::type t, bool ditchpcm=false);
// randomise the synaptic pointer
void jiggle();
class sound {
public:
sound(const std::string &name, const sample &sample) :
@ -70,7 +73,8 @@ public:
// synaptic search
double calc_average_diff(search_params &params);
void build_synapses(search_params &params, double threshold);
void build_synapses_thresh(search_params &params, double threshold);
void build_synapses_fixed(search_params &params);
u32 search_synapses(const block &target, search_params &params);
double get_current_error() { return m_current_error/m_average_error; }

View File

@ -54,45 +54,61 @@ void run_audio(void* c, unsigned int frames) {
int main(int argc, char *argv[])
{
unit_test();
// unit_test();
u32 len=1000;
search_params p(0.5,0,0,99,0);
cerr<<"starting"<<endl;
brain source, target;
/* {
brain source;
// source.load_sound("../sound/source/shostakovich6.wav");
// source.load_sound("../sound/source/808.wav");
source.load_sound("../sound/source/joey.wav");
source.load_sound("../sound/source/pw2.wav");
// source.load_sound("../sound/source/joey.wav");
// source.load_sound("../sound/source/pw2.wav");
// source.load_sound("../sound/source/pw3.wav");
source.load_sound("../sound/source/claps.wav");
// source.load_sound("../sound/source/claps.wav");
// source.load_sound("../sound/source/eagle.wav");
// source.load_sound("../sound/source/rise.wav");
// source.load_sound("../sound/source/totalsine.wav");
// target.load_sound("../sound/source/apache.wav");
// source.load_sound("../sound/source/rise.wav");
// source.load_sound("../sound/source/totalsine.wav");
//target.load_sound("../sound/source/sailingbybit.wav");
// source.load_sound("../sound/source/sailingbybit.wav");
// source.load_sound("../sound/source/dreambit.wav");
source.load_sound("../sound/source/KONG.WAV");
source.load_sound("../sound/source/BIRD.WAV");
source.load_sound("../sound/source/CCBEGIN.WAV");
source.load_sound("../sound/source/cc-end.wav");
source.load_sound("../sound/source/cc-extra.wav");
source.load_sound("../sound/source/cc-high.wav");
source.load_sound("../sound/source/cc-magic.wav");
source.load_sound("../sound/source/cc-start.wav");
source.load_sound("../sound/source/cc-warp.wav");
target.load_sound("../sound/source/apache.wav");
//target.load_sound("../sound/source/dreambit.wav");
//target.load_sound("../sound/source/sb-left.wav");
//target.load_sound("../sound/source/rise.wav");
cerr<<"loaded sounds"<<endl;
cerr<<endl;
u32 len=3000;
source.init(len,len-len,window::HANN);
target.init(len,len-len/8,window::HANN);
cerr<<"synapse stuff"<<endl;
cerr<<"synapse stuff"<<endl;
cerr<<"synapse stuff"<<endl;
search_params p(0.5,0,0,99,0);
source.build_synapses(p,0.9);
source.build_synapses_fixed(p);
source.set_usage_falloff(0.9);
ofstream of("8bit.brain",ios::binary);
of||source;
of.close();
}
*/
brain source;
ifstream ifs("8bit.brain",ios::binary);
ifs||source;
ifs.close();
brain target;
target.load_sound("../sound/source/apache.wav");
//target.load_sound("../sound/source/pw2.wav");
//target.load_sound("../sound/source/sailingbybit.wav");
target.init(len,len-len/8,window::HANN);
cerr<<"ready..."<<endl;
cerr<<"we have "<<source.get_num_blocks()<<" brain blocks ("<<source.get_num_blocks()*len/44100.0<<" secs)"<<endl<<endl;
@ -102,8 +118,9 @@ int main(int argc, char *argv[])
renderer rr(source,target);
rr.set_playing(true);
rr.get_params()->m_ratio=0.5;
rr.get_params()->m_ratio=0;
rr.get_params()->m_usage_importance=0.5;
rr.get_params()->m_num_synapses=150;
rr.set_slide_error(5.5);
rr.set_search_algo(renderer::SYNAPTIC);

View File

@ -43,6 +43,7 @@ void renderer::reset() {
m_render_time=0;
m_target_index=0;
m_render_blocks.clear();
m_source.jiggle();
}
void renderer::process(u32 nframes, float *buf) {

View File

@ -26,13 +26,16 @@ public:
m_n_ratio(n_ratio),
m_fft1_start(s1),
m_fft1_end(e1),
m_usage_importance(usage_importance) {}
m_usage_importance(usage_importance),
m_num_synapses(100)
{}
float m_ratio;
float m_n_ratio;
int m_fft1_start;
int m_fft1_end;
u32 m_fft1_start;
u32 m_fft1_end;
float m_usage_importance;
u32 m_num_synapses;
};
}