File: WalshHadamard.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (153 lines) | stat: -rw-r--r-- 2,647 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 *  WalshHadamard.cpp
 *  xSC3ExtPlugins-Universal
 *
 *  Created by Nick Collins on 10/04/2009.
 *  Copyright 2009 Nick Collins. All rights reserved.
 *
 */

///#include "WalshHadamard.h"
#include "NCAnalysis.h"



//find maximum value within last x blocks
struct WalshHadamard : public Unit {
	//float * store;
	int m_log2, m_size, m_count; //
	float * m_data;
};



extern "C"
{
	//required interface functions
	void WalshHadamard_next(WalshHadamard *unit, int wrongNumSamples);
	void WalshHadamard_Ctor(WalshHadamard *unit);
	void WalshHadamard_Dtor(WalshHadamard *unit);
}



//from music DSP list site: http://www.musicdsp.org/showArchiveComment.php?ArchiveID=18

void inline wht_bfly (float& a, float& b);
int inline l2 (long x);
void FWHT(float * data, int log2);

void inline wht_bfly (float& a, float& b)
{
	float tmp = a;
	a += b;
	b = tmp - b;
}

// just a integer log2
int inline l2 (long x)
{
	int l2;
	for (l2 = 0; x > 0; x >>=1)
	{
		++ l2;
	}

	return (l2);
}

////////////////////////////////////////////
// Fast in-place Walsh-Hadamard Transform //
////////////////////////////////////////////

void FWHT(float * data, int log2)
{
	for (int i = 0; i < log2; ++i) {
		for (int j = 0; j < (1 << log2); j += 1 << (i+1)) {

			for (int k = 0; k < (1 << i); ++k) {
				wht_bfly (data [j + k], data [j + k + (1 << i)]);
			}

		}
	}
}


void WalshHadamard_Ctor( WalshHadamard* unit ) {

	//int msamp= (int) ZIN0(1);
	unit->m_size= 64;
	unit->m_log2 = l2 (unit->m_size) - 1;
	unit->m_data=  (float*)RTAlloc(unit->mWorld, unit->m_size * sizeof(float));

	unit->m_count=0;

	SETCALC(WalshHadamard_next);

}

void WalshHadamard_Dtor(WalshHadamard *unit) {
	RTFree(unit->mWorld, unit->m_data);
}

void WalshHadamard_next( WalshHadamard *unit, int inNumSamples ) {

	int j;
	float *in = IN(0);
	float* out = OUT(0);

	//float *out = ZOUT(0);

	//only to be used at .kr
	//printf("samp to calc %d", inNumSamples);

	//come back to while loop
//	int siz = unit->m_size; //unit->mWorld->mFullRate.mBufLength;
//    int left= siz- unit->m_count;

	//ASSUMES block size of 64!

	float * data= unit->m_data;

	for(j=0; j<inNumSamples; ++j) {
		data[j] = in[j];
	}


	//forwards WH
	FWHT(data, unit->m_log2);

	int which= ZIN0(1);

	//processing (zero every other one)
	for(j=0; j<which; ++j) {
		//data[2*j+1] =0.0;
		data[j] =0.0;
	}

	//inverse WH
	FWHT(data, unit->m_log2);

	//output
	for(j=0; j<inNumSamples; ++j) {
		out[j] =  data[j]*0.015625;
	}

//	unit->m_count= left;

}


void loadWalshHadamard(InterfaceTable *inTable)
{

	//ft= inTable;

	DefineDtorCantAliasUnit(WalshHadamard);

}

//void datafun( WalshHadamard *unit) {
//
//}