Posts Tagged MATLAB

Octave and MATLAB

Since I can’t afford MATLAB for my own machine. I’m making an effort to make my code OCTAVE-compatible, so I will be able to reuse the code on my own computer. This will involve creating some toolboxes I bet; along with cataloging the differences.

Differences

  • fopen.m, ‘ieee-le.l64′ does not exist in ocatve as an arch, use ‘ieee-le’ instead.
  • daspect.m does not exist, make a function that does a set(gca,’dataaspectratio’,x);

No Comments

Old school Nokia ringtones

I found some really old code today. In the early days of the cellphone before middle schoolers and elementary schoolers had cellphones, the ringtones for Nokia phones were input using an ancient code.

Here’s the zelda theme, which I scrapped off a website a long time ago.

4#a1, 4.f1, 8#a1, 16#a1, 16c2, 16d2, 16#d2, 2f2, 8-, 8f2, 16.f2, 16#f2, 16.#g2,
2.#a2, 16.#a2, 16#g2, 16.#f2, 8.#g2, 16.#f2, 2f2, 4f2, 8#d2, 16#d2, 16f2, 2#f2,
8f2, 8#d2, 8#c2, 16#c2, 16#d2, 2f2, 8#d2, 8#c2, 8c2, 16c2, 16d2, 2e2, 4g2, 8f2,
16f1, 16f1, 8f1

Here’s some MATLAB/OCTAVE code

function nokia2wav (filename, tempo)
Fs = 44.1e3;
if (nargin == 0)
  filename = 'zelda.txt';
end
if (nargin < 2)
  tempo = 125;
end

% read in file and turn it into a long string
fid = fopen(filename);
notebuf = '';
while feof(fid) == 0
   notebuf = [notebuf fgetl(fid)];
end
fclose(fid);

% convert to wav
y = NOKIAtoWAV(notebuf, tempo, Fs);

% write wav file
[buf rem] = strtok(filename, '.');
wavfilename = [buf '.wav'];
wavwrite(y', Fs, wavfilename);

function y = NOKIAtoWAV (str, tempo, Fs)
%NOKIATOWAV converts a string of nokia text into a wavefile
%
% y = NOKIAtoWAV (str, tempo, Fs)
%
% input:
%   str - nokia text string
%   tempo - beats per minute
%   Fs - sampling frequency
%
% output:
%   y - waveform
%

% parse the string to individual NOKIA notes
notes = words(str,',');

% convert each NOKIA note into a frequency and duration
for m = 1:length(notes)
    [midi(m) t(m)] = analyzeNOKIAnote(notes(m,:), tempo);
end

% massage data
midi = midi';
t = t';

% assignment allocation
nend = length(midi);
y = 0;
p = 0;

% keep adding next note to waveform
for n = 1:nend
    y = [y generatenote(MIDItoFreq(midi(n)), t(n), p, Fs)];
    p = asin(y(length(y)));
end

%------------------------------------------------------------------------------
% String Tokenizer
%------------------------------------------------------------------------------

function allWords = words(inputString, delim)
%WORDS parses string by delimiter
%
% allWords = words(inputString, delim)
%
% input:
%   inputString - input string
%   delim - delimiters
% output:
%   allwords - array of parsed strings
%

    remainder = inputString;
    allWords = '';
while (any(remainder))
    [chopped,remainder] = strtok(remainder, delim);
    allWords = strvcat(allWords, chopped);
end

%-------------------------------------------------------------------------------
% Turn NOKIA string into freq and duration
%-------------------------------------------------------------------------------

function [midi t] = analyzeNOKIAnote(str, tempo)
%analyzeNOKIAnote
%
% [midi t] = analyzeNOKIAnote(str, tempo)
%
% input:
%   str - NOKIA note
%   tempo - beats per minute
%
% output:
%   midi - frequency
%   t - duration
%

% split duration and frequency
[duration note] = strtok(str,'abcdefg#-');
t0 = 4*60/tempo;

% compute duration
[duration onehalf] = strtok(duration,'.');
t = t0*1/str2double(duration);
midi = 100;
if(length(onehalf));
    t = 1.5*t;
end

% compute frequency
note = strtrim(note);
if strcmp(note,'c1')
    midi = 60;
elseif strcmp(note,'#c1');
    midi = 61;
elseif strcmp(note,'d1');
    midi = 62;
elseif strcmp(note,'#d1');
    midi = 63;
elseif strcmp(note,'e1');
    midi = 64;
elseif strcmp(note,'f1');
    midi = 65;
elseif strcmp(note,'#f1');
    midi = 66;
elseif strcmp(note,'g1');
    midi = 67;
elseif strcmp(note,'#g1');
    midi = 68;
elseif strcmp(note,'a1');
    midi = 69;
elseif strcmp(note,'#a1');
    midi = 70;
elseif strcmp(note,'b1');
    midi = 71;
elseif strcmp(note,'c2');
    midi = 72;
elseif strcmp(note,'#c2');
    midi = 73;
elseif strcmp(note,'d2');
    midi = 74;
elseif strcmp(note,'#d2');
    midi = 75;
elseif strcmp(note,'e2');
    midi = 76;
elseif strcmp(note,'f2');
    midi = 77;
elseif strcmp(note,'#f2');
    midi = 78;
elseif strcmp(note,'g2');
    midi = 79;
elseif strcmp(note,'#g2');
    midi = 80;
elseif strcmp(note,'a2');
    midi = 81;
elseif strcmp(note,'#a2');
    midi = 82;
elseif strcmp(note,'b2');
    midi = 83;
elseif strcmp(note,'-');
    midi = 0;
else
    note;
end
midi = midi;

function f = MIDItoFreq(MIDInum)
%MIDItoFreq gets the frequency of a MIDI note number
%
%   f = MIDItoFreq(MIDInum)
%
%   input:
%       MIDInum - MIDI note number
%
%   output:
%       f - frequency of MIDI note number
%

f = 440*(2^(1/12).^(MIDInum-69));

function y = generatenote(f, t, p, Fs)
%generatenote generates a wav of a certain frequency and duration
%
%   y = generatenote(f, t, p, Fs)
%
%   input:
%       f - frequency
%       t - duration
%       p - phase shift
%       Fs - sampling frequency (usually 44.1 kHz for audio)
%
%   output:
%       y - waveform
%

% t in seconds
t = 0:Fs*t;
y = sin(2*pi*f/Fs*t+p);
% add fade to eliminate pops
x0 = round(10e-3*Fs);
y(end-x0+1:end) = y(end-x0+1:end).*linspace(1,0,x0);

No Comments

ICC Profile

I’m too cheap to pay lots of money for color management solutions from X-rite. I already have a gretag-macbeth colorchecker chart, so that’s a sunk cost. I originally planned to use it to calibrate my scanner and camera. I wrote some MATLAB code to analyze the scanned image and did some tweaking on the settings to improve the color, but it was time consuming, because I had to do it iteratively. I looked at the documentation for the ICC profile and I think I can write some MATLAB code to adjust the colors. I think this project usurps the astrolabe, because I haven’t bought the astrolabe book required to design an astrolabe. I also want to scan some old pictures during the christmas break. I’ll put up the MATLAB code for it and try to make some money by putting an affiliate link to amazon for the color checker target.

,

No Comments

Learning to Program or Just Learning

So I had a young family member ask me to debug his program, because he was learning to program in C++. I asked him why he wanted to learn how to program. He replied it seemed like a good thing to know. It is a good thing to know, but it shouldn’t be your motivation for learning something. There are a lot of things in the world to learn. One should learn things that will help them accomplish their goals.

When I was a young, I learned REXX scripting to make it easier to play BBS door games using ZOC. I learned C++, MFC and DirectX, because I wanted to program video games. Later I learned OpenGL and OpenAL too since I wanted to make games for any platform. I learned HTML and Java, because I wanted to make websites with applets. I learned Flash, because I wanted to make a cool intro to my website and maybe a video game. I learned Objective-C to program iPhone games. I learned MATLAB, because I can throw down stuff and analyze things quickly. I learned PHP, because I wanted to make a database driven website. All of these computer related learnings revolve around accomplishing a task.

You learn something to be able to do something, not just to do it. When you want to do something with the knowledge you gain it makes you eager to gain that knowledge. I taught two weekend courses to high school. Those kids signed up for my course because they had specific things they wanted to learn. This made them more attentive and better learners. In high school I wasn’t too fond of biology, but in graduate school I was eager to learn all I could, because it was the right time for me to learn. I was interested in the subject. Some basic knowledge in broad subject areas is needed, but I think K-12 education should be focused on fostering learning skills and feeding the appetite to learn. Schools are pretty rigid due to limited resources, ingrained doctrine and maybe some other things that I am unable to name.

I’ve had discussions about education and school along similar veins. People want to go straight to the cool stuff. Other things are just boring and make students lose interest before you get to the cool part. As a child, one may feel forced to learn how to play the piano or learn a foreign language, but when you get older it’s harder to learn to play the piano or a foreign language when you actually want to travel and use the foreign language or have a jam session. I also think a part of feeling forced to learn is that the parents don’t play an instrument or speak said foreign language at home. If it was a part of your life, then it would feel more natural. One needs to cultivate an environment that makes learning a necessity.

No Comments

MATLAB – Transpose, Hermitian Conjugate

I wasted two days, because I wanted a transpose, but I got a hermitian conjugate instead when I was doing some calculations with complex numbers. The ‘ symbol does a hermitian conjugate and not solely a transpose. The dot() function also uses the ‘ symbol which does a hermitian conjugate instead of a transpose. You should always know what your functions do. If you want a transpose of a matrix, you can use .’ (dot prime) instead of ‘ (prime).

1 Comment